Home Forums HAast (High Availability for Asterisk) API Programming Cluster status via socket Reply To: Cluster status via socket

Avatar photoTelium Support Group
Participant
Post count: 262

The best way to communicate programmatically with HAAst is through the socket interface. When haast is running it creates a socket in the directory /var/run. This socket looks and acts just like a regular file; you can write to the file and read from the file to command HAAst and gather status information.

To retrieve the status of the cluster via the socket interface, use the ‘getstatus’ command. The pseudocode to do so is as follows:


Open file /var/run/haast.sock as read+write
Read ‘ready>’ prompt from file
Optionally write command ID and newline to file, e.g. ‘id: 123n’
Write get status command and newline to file, e.g. ‘command:getstatus\n\n’
Read response from file
Close file
Parse response

The ID sent can be any string (to uniquely identify the response) but is optional. In general we recommend sending an auto-incremented counter. Once all required parameters (of this command) have been sent, send a second newline (i.e. 2 sequential newline characters) to tell HAAst the command is complete and ready for processing.

The following is the actual output (read) in red color, and input (written) in blue color, from the above pseudo code:

haast socket interface v1.1
ready>id: 123
command:getstatus

id: 123
result: ok
cluster name: Telium
cluster start time: 1459816998033
cluster start time formatted: Mon Apr 4 20:43:18 2016
cluster run duration: 12404571
cluster run duration formatted: 71 days, 13 hours, 42 minutes, 51 seconds
cluster fail over count: 79
local peer name: PBX1 in Waterloo data center wall
local haast state: 4
local haast state formatted: Active
local peer health state: 1
local peer health state formatted: Normal
local asterisk state: 2
local asterisk state formatted: Started
local asterisk connection state: 7
local asterisk connection state formatted: Logged in
local start time: 1464643147663
local start time formatted: Mon May 30 17:19:07 2016
local haast run duration: 2740022
local haast run duration formatted: 15 days, 17 hours, 7 minutes, 2 seconds
local fail over count: 11
local previous fail over timestamp: 1465102805740
local previous fail over timestamp formatted: Sun Jun 5 01:00:05 2016
local previous fail over cause: 2
local previous fail over cause formatted: Dual standby peer contention detected
local previous haast state: 3
local previous haast state formatted: Standby
local to remote peerlink connection state: 7
local to remote peerlink connection state formatted: Up
remote data available: 1
remote peer name: PBX2 VM in Mississauga data center
remote haast state: 3
remote haast state formatted: Standby
remote peer health state: 1
remote peer health state formatted: Normal
remote asterisk state: 4
remote asterisk state formatted: Stopped
remote asterisk connection state: 4
remote asterisk connection state formatted: Connect failed
remote start time: 1464643407951
remote start time formatted: Mon May 30 17:23:27 2016
remote haast run duration: 2739761
remote haast run duration formatted: 15 days, 17 hours, 2 minutes, 41 seconds
remote fail over count: 8
remote previous fail over timestamp: 1465102804675
remote previous fail over timestamp formatted: Sun Jun 5 01:00:04 2016
remote previous fail over cause: 23
remote previous fail over cause formatted: Local peer automatic demotion request
remote previous haast state: 4
remote previous haast state formatted: Active
remote to local peerlink connection state: 7
remote to local peerlink connection state formatted: Up
ready>

To learn how to use the HAAst socket interface you can use the ‘socat’ command to interact with the HAAst socket from a Bash command line. (You may need to add the socat command / package to your particular Linux installation). The exact syntax for the socat command is as follows:


socat – UNIX-CONNECT:/var/run/haast.sock

You can then send any invalid command (eg: XXXX) and HAAst will respond with a list of valid commands.