This is not exactly what you want to do but it should give you the general idea. This is how I switch to the backup repeater when the main fails. This script runs from cron every minute.
cat testSantiagoSwitchRTCM.sh
#!/bin/sh
voter=$(./timvot 127.0.0.1 2532)
#echo $voter
if echo "$voter" | grep -q Santiago
then
echo "Santiago is up."
if [ -e theDownFile ]
then
/bin/logger "Santiago up. Switching HB RTCM TX off."
./RTCM_hb_tx_off.sh
rm -f theDownFile
fi
else
echo "Santiago is down."
if [ ! -e theDownFile ]
then
/bin/logger "Santiago down. Switching HB RTCM TX on."
touch theDownFile
./RTCM_hb_tx_on.sh
fi
fi
exit 0
The c program timvote simply queries asterisk via the AMI for the status of the RTCM. This would be way easier to do in php but Jim Dixon wrote this and he was a c/emacs sort of a guy who wouldn’t dream of touching php. The username and password are hard coded (BAD JIM!) and are replaced with XXXXX here. If you dig in to my allmon code you see how to do this in php. Of course you could this as is replacing the username and password and compile.
cat timvot.c
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define RCVBUFSIZE 256 /* Size of receive buffer */
char rxbuf[40960];
static void DieWithError(char *errorMessage)
{
fprintf(stderr,errorMessage);
exit(255);
}
void readstuff(int sock)
{
int i,bytesRcvd;
char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */
fd_set fds[2];
struct timeval tmout;
rxbuf[0] = 0;
while(1) {
FD_ZERO(fds);
FD_SET(sock,fds);
tmout.tv_sec = 0;
tmout.tv_usec = 100000;
i = select(sock + 1,fds,NULL,NULL,&tmout);
if (i < 1) break;
if (!FD_ISSET(sock,fds)) continue;
if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) < 0)
DieWithError("recv() failed or connection closed prematurely");
if (!bytesRcvd) break;
echoBuffer[bytesRcvd] = 0;
if ((strlen(rxbuf) + strlen(echoBuffer)) < sizeof(rxbuf)) strcat(rxbuf,echoBuffer);
}
return;
}
int main(int argc, char *argv[])
{
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
unsigned short echoServPort; /* Echo server port */
char *servIP; /* Server IP address (dotted quad) */
char echoString[1024]; /* String to send to echo server */
unsigned int echoStringLen; /* Length of string to echo */
if ((argc < 3) || (argc > 4)) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s <Server IP> <NodeList> [<Server Port>]\n",
argv[0]);
exit(1);
}
servIP = argv[1]; /* First arg: server IP address (dotted quad) */
if (argc == 4)
echoServPort = atoi(argv[3]); /* Use given port, if any */
else
echoServPort = 5038; /* 7 is the well-known port for the echo service */
/* Create a reliable, stream socket using TCP */
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("socket() failed");
/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */
/* Establish the connection to the echo server */
if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError("connect() failed");
strcpy(echoString,"ACTION: LOGIN\r\nUSERNAME: XXXXXXXX\r\nSECRET: XXXXXXXXXX\r\nEVENT: 0\r\n\r\n");
echoStringLen = strlen(echoString); /* Determine input length */
/* Send the string to the server */
if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
DieWithError("send() sent a different number of bytes than expected");
readstuff(sock);
if ((!strstr(rxbuf,"Response: Success")) ||
(!strstr(rxbuf,"Message: Authentication accepted")))
DieWithError(rxbuf);
printf("Logged In\n");
sprintf(echoString,"ACTION: VoterStatus\r\nNODE: %s\r\n\r\n",argv[2]);
echoStringLen = strlen(echoString); /* Determine input length */
/* Send the string to the server */
if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
DieWithError("send() sent a different number of bytes than expected");
readstuff(sock);
if (!rxbuf[0]) DieWithError("Cannot do command");
printf("Command Returned: %s\n",rxbuf);
close(sock);
exit(0);
}