Skip to content

Commit

Permalink
- added switch to return nagios compatible output
Browse files Browse the repository at this point in the history
- added exitcodes - 0 ok; 2 error
  • Loading branch information
waringer committed Jun 22, 2013
1 parent 206d9de commit 9a264c9
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions HomePlugAVList.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,30 +310,34 @@ void usage(void) {

printf("%s",
"\nHomePlug-AV Device List version " HomePlugAVList_VERSION " by Holger Wolff <waringer@gmail.com>\n\n"
"Usage: HomePlugAVList [-h] [-b device] [-m mac] [-c count] interface\n\n"
"Usage: HomePlugAVList [-h] [-n] [-b device] [-m mac] [-c count] interface\n\n"

" -b device use device (default is /dev/bpf0)\n"
" -m mac mac to use (default is to search for a mac)\n"
" -c count how many times try to connect if no response is received (default is 5)\n"
" -c count how many times try to connect if no response is received (default is 5)\n"
" -n Nagios compatible output\n"
" -h display this help\n\n"

" ...\n\n");
}

void ParseOptions(int argc, char *argv[], char *bpfn, char *ifname, u_char *DeviceMac, u_int *TryCount)
void ParseOptions(int argc, char *argv[], char *bpfn, char *ifname, u_char *DeviceMac, u_int *TryCount, int *UseNagiosFormat)
{
int ch, i;
u_char mac[18];

/* Parse command line options */
while ((ch = getopt(argc, argv, "b:m:c:h")) != -1) {
while ((ch = getopt(argc, argv, "b:m:c:hn")) != -1) {

switch (ch) {
case 'b':
strncpy(bpfn, optarg, 32);
break;
case 'c':
sscanf(optarg, "%3u", TryCount);
break;
case 'n':
*UseNagiosFormat = 1;
break;
case 'm':
strncpy(mac, optarg, 17);
Expand Down Expand Up @@ -388,13 +392,15 @@ int main(int argc, char *argv[]) {
u_char SenderMac[6] = {0,0,0,0,0,0};
char DeviceVersion[200] = "";
char macbuf[20] = "";
u_int TryCounter = 1;
u_int TryCounter = 1;
int UseNagiosFormat = 0;
int ExitCode = 0;
u_int MaxTrys = 5;
u_int i, j;

HomePlugNetInfo.NetworkCount = 0;

ParseOptions(argc, argv, bpfn, ifname, SenderMac, &MaxTrys);
ParseOptions(argc, argv, bpfn, ifname, SenderMac, &MaxTrys, &UseNagiosFormat);

SetupNetDevice(bpfn, &net, ifname);

Expand All @@ -405,9 +411,12 @@ int main(int argc, char *argv[]) {


if ((SenderMac[0] != 0) || (SenderMac[1] != 0) || (SenderMac[2] != 0) || (SenderMac[3] != 0) || (SenderMac[4] != 0) || (SenderMac[5] != 0))
{
{
if (0 == UseNagiosFormat)
{
printf("- Device MAC :\t\t\t\t%s\n", format_mac_addr(SenderMac, macbuf));
printf("- Device Version :\t\t\t%s\n", DeviceVersion);
}

TryCounter = 1;
do
Expand All @@ -420,6 +429,8 @@ int main(int argc, char *argv[]) {
{
char NetID_Buffer[23] = "";

if (0 == UseNagiosFormat)
{
printf("- Network count :\t\t\t%02x\n", HomePlugNetInfo.NetworkCount);
for(i = 0; i < HomePlugNetInfo.NetworkCount; i++)
{
Expand All @@ -438,11 +449,21 @@ int main(int argc, char *argv[]) {
printf("- Network %02u Station %02u AvgPhyTX Rate :\t%02x\n", i, j, HomePlugNetInfo.Networks[i].Stations[j].AvgPhyTXRate);
printf("- Network %02u Station %02u AvgPhyRX Rate :\t%02x\n", i, j, HomePlugNetInfo.Networks[i].Stations[j].AvgPhyRXRate);
}
}
}
}
else
{
printf("OK - [%s]\n", DeviceVersion);
}
}
else
{
printf("No devices found!\n");
if (0 == UseNagiosFormat)
printf("No devices found!\n");
else
printf("CRITICAL - No devices found!\n");

ExitCode = 2;
}

/* Free memory */
Expand All @@ -453,5 +474,7 @@ int main(int argc, char *argv[]) {
if (HomePlugNetInfo.NetworkCount > 0)
free(HomePlugNetInfo.Networks);

CloseNetDevice(&net);
CloseNetDevice(&net);

exit(ExitCode);
}

0 comments on commit 9a264c9

Please sign in to comment.