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

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • Avatar photoCustomer Inquiry
    Member
    Post count: 197

    I am writing a C++ program and wish to determine the status of a HAAst peer. How should I get this information from HAAst?

    Avatar photoTelium Support Group
    Member
    Post count: 258

    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.

    Avatar photoTelium Support Group
    Member
    Post count: 258

    To help customers trying to extract status information, here is a sample python script that retrieves and prints the local status:


    # Example python script to retrieve local HAAst status
    import socket
    import sys
    # End of packet marker
    READYPROMPT=’ready>’
    # Create a UDS socket
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    # Connect the socket to the port where the server is listening
    server_address = ‘/run/haast.sock’
    try:
    sock.connect(server_address)
    except socket.error, msg:
    print >>sys.stderr, msg
    sys.exit(1)
    # Wait for a packet
    def receivepacket():
    global sock
    total_data=[];data=”
    while True:
    data=sock.recv(8192)
    if READYPROMPT in data:
    total_data.append(data[:data.find(READYPROMPT)])
    break
    total_data.append(data)
    if len(total_data)>2:
    #check if end_of_data was split
    last_pair=total_data[-2]+total_data[-1]
    if READYPROMPT in last_pair:
    total_data[-2]=last_pair[:last_pair.find(READYPROMPT)]
    total_data.pop()
    break
    return ”.join(total_data).replace(‘r’,”).replace(‘nn’,’n’)
    # Send a packet
    def sendpacket(message):
    global sock
    success = 1
    try:
    # Send data
    message += ‘nn’
    # print >>sys.stderr, ‘sending “%s”‘%message
    sock.sendall(message)
    amount_received = 0
    amount_expected = len(message)
    while amount_received < amount_expected: data = sock.recv(16) amount_received += len(data) finally: # print >>sys.stderr, ‘closing socket’
    # sock.close()
    success = 0
    return success
    got = receivepacket()
    sendpacket(“id:123ncommand:getstatus”)
    got = receivepacket()
    for item in got.split(“n”):
    if “local haast state formatted:” in item:
    print item.strip()
    sock.close()

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.