Simple Bash script to export active channels
, active calls
and calls processed
to Stackdriver Custom Metrics.
NB You have to create Asterisk custom metrics within your GCP Console projects before deploying this script.
the easier way is to use metricDescriptors.create unless you're a curl pro 😎
Then use below values:
name
: projects/[YOUR-GCP-PROJECT-ID]
{
"name": "",
"description": "Active channels in all media servers.",
"displayName": "active channels",
"type": "custom.googleapis.com/active_channels",
"metricKind": "GAUGE",
"valueType": "DOUBLE",
"unit": "{channel}",
"labels": [
{
"key": "project_id",
"valueType": "STRING",
"description": "The identifier of the GCP project associated with this resource (e.g., {your-project-id})."
},
{
"key": "instance_id",
"valueType": "STRING",
"description": "The VM instance identifier assigned by GCE."
},
{
"key": "zone",
"valueType": "STRING",
"description": "The GCE zone in which the VM is running."
},
],
}
{
"name": "",
"description": "Active calls in all media servers.",
"displayName": "active calls",
"type": "custom.googleapis.com/active_calls",
"metricKind": "GAUGE",
"valueType": "DOUBLE",
"unit": "{call}",
"labels": [
{
"key": "project_id",
"valueType": "STRING",
"description": "The identifier of the GCP project associated with this resource (e.g., {your-project-id})."
},
{
"key": "instance_id",
"valueType": "STRING",
"description": "The VM instance identifier assigned by GCE."
},
{
"key": "zone",
"valueType": "STRING",
"description": "The GCE zone in which the VM is running."
},
],
}
{
"name": "",
"description": "Calls processed in all media servers.",
"displayName": "calls processed",
"type": "custom.googleapis.com/calls_processed",
"metricKind": "GAUGE",
"valueType": "DOUBLE",
"unit": "{call}",
"labels": [
{
"key": "project_id",
"valueType": "STRING",
"description": "The identifier of the GCP project associated with this resource (e.g., {your-project-id})."
},
{
"key": "instance_id",
"valueType": "STRING",
"description": "The VM instance identifier assigned by GCE."
},
{
"key": "zone",
"valueType": "STRING",
"description": "The GCE zone in which the VM is running."
},
],
}
To check Metrics
name
:
- projects/[YOUR-GCP-PROJECT-ID]/metricDescriptors/custom.googleapis.com/active_calls
- projects/[YOUR-GCP-PROJECT-ID]/metricDescriptors/custom.googleapis.com/active_channels
- projects/[YOUR-GCP-PROJECT-ID]/metricDescriptors/custom.googleapis.com/calls_processed
To delete Metrics
name
:
- projects/[YOUR-GCP-PROJECT-ID]/metricDescriptors/custom.googleapis.com/calls_processed
- projects[YOUR-GCP-PROJECT-ID]metricDescriptors/custom.googleapis.com/active_calls
- projects/[YOUR-GCP-PROJECT-ID]/metricDescriptors/custom.googleapis.com/active_channels
Maualy create timeSeries points
use monitoring.projects.timeSeries.create
name
:projects/[YOUR-GCP-PROJECT-ID]
- Calls Processed
Request body
{
"timeSeries":
[
{
"metric":
{
"type": "custom.googleapis.com/calls_processed"
},
"resource":
{
"type": "gce_instance",
"labels":
{
"instance_id": "[GCP-INSTANCE-ID]",
"zone": "[GCP-ZONE]",
"project_id": "[YOUR-GCP-PROJECT-ID]"
}
},
"points":
[
{
"interval":
{
"endTime": "2018-02-20T13:45:20.187Z"
},
"value":
{
"doubleValue": 101
}
}
]
}
]
}
- Active calls
Request body
{
"timeSeries":
[
{
"metric":
{
"type": "custom.googleapis.com/active_calls"
},
"resource":
{
"type": "gce_instance",
"labels":
{
"instance_id": "[GCP-INSTANCE-ID]",
"zone": "[GCP-ZONE]",
"project_id": "[YOUR-GCP-PROJECT-ID]"
}
},
"points":
[
{
"interval":
{
"endTime": "2018-02-20T13:45:20.187Z"
},
"value":
{
"doubleValue": 10
}
}
]
}
]
}
- Active channels
Request body
{
"timeSeries":
[
{
"metric":
{
"type": "custom.googleapis.com/active_channels"
},
"resource":
{
"type": "gce_instance",
"labels":
{
"instance_id": "[GCP-INSTANCE-ID]",
"zone": "[GCP-ZONE]",
"project_id": "[YOUR-GCP-PROJECT-ID]"
}
},
"points":
[
{
"interval":
{
"endTime": "2018-02-20T13:45:20.187Z"
},
"value":
{
"doubleValue": 20
}
}
]
}
]
}
- Download
asterisk_stat.sh
in your gcp instance running Asterisk. - Edit the script to change this part in the beginning:
# Replace with yours
gcp_project_id="YOUR-GCP-PROJECT-ID"
gcp_instance_id="GCP-INSTANCE-ID"
gcp_zone="GCP-ZONE"
- Add a cron job in your asterisk instance
/etc/cron.d/asterisk-start
PATH=/sbin:/usr/sbin:/bin:/usr/bin
# Start job every 5 minutes
*/5 * * * * root /etc/asterisk/bin/asterisk_stat.sh >> /var/log/cron.log 2>&1
- Setting up the Asterisk HTTP server
/etc/asterisk/http.conf
;http.conf
[general]
enabled=yes
bindaddr=0.0.0.0
bindport=8088
tlsenable=yes
tlsbindaddr=0.0.0.0:7443
tlscertfile=/etc/asterisk/keys/asterisk.pem
tlsprivatekey=/etc/asterisk/keys/asterisk.pem
enablestatic=yes
servername=Rebtel Media Gateway
;redirect = / /httpstatus
The static html page [astdatadir]/static-http/current-status.html
will be generated if asterisk is running otherwise the script wil remove it.
NB For the log rotation, check that the file /var/log/cron.log
is including in /etc/logrotate.d/syslog
Only bash
, curl
and asterisk
.
- Creating and Enabling Service Accounts for Instances
- Creating Custom Metrics
- Using Custom Metrics
- OAuth 2.0 for Client-side Web Applications
- Create a dashboard and chart
Todo