watchport/w water sensor monitoring with linux (zabbix)

We have a watchport/w sensor and I wanted to start monitoring it with zabbix. I did a little googling and the only thing this sensor does is set the DCD line on a usb modem to high when there is water, otherwise it's low (not connected).

I couldn't find anything to test for the carrier easily (cu maybe, but it seems a bit clumsy). I found on the tty_ioctl man page the code to do this in C, so I just modified it and made a little app to check the port.


EXAMPLE (from tty_ioctl manpage)
Check the condition of DTR on the serial port.

#include
#include
#include

int
main(void)
{
int fd, serial;

fd = open("/dev/ttyS0", O_RDONLY);
ioctl(fd, TIOCMGET, &serial);
if (serial & TIOCM_DTR)
puts("TIOCM_DTR is not set");
else
puts("TIOCM_DTR is set");
close(fd);
}

Taking the example and changing the check for DTR to a check for DCD...


#include
#include
#include
#include
#include
#include
#include
#include

void usage(char** argv)
{
printf("usage: %s device\n", argv[0]);
printf(" check the dcd on device\n");
exit(1);
}

int main (int argc, char* argv[])
{
int fd;
int serial;

if (argc != 2) usage(argv);

struct stat sb;
if (stat(argv[1],&sb) == -1) {
printf("Error: Couldn't stat %s\n",argv[1]);
exit(1);
}
if (S_ISCHR(sb.st_mode) != 1) {
printf("Error: %s is not a character device\n",argv[1]);
exit(1);
}
fd = open(argv[1],'r');
if (fd == -1) {
printf("Error: Couldn't open %s\n",argv[1]);
exit(1);
}

serial = 0;
if(ioctl(fd, TIOCMGET, &serial) < 0)
{
printf("Error: ioctl() failed on %s: %d: %s\n", argv[1],errno, strerror(errno));
exit(1);
}
printf("%d\n",serial & TIOCM_CAR ? 1 : 0);
exit(0);
}

I put in a few extra checks to see if the file is a character device etc. compile with gcc like gcc -o dcdcheck dcdcheck.c To Run, just give it a character device to work with


[thomas@work scratch]$ ./dcdcheck /dev/ttyUSB0
Error: Couldn't open /dev/ttyUSB0
[thomas@work scratch]$ sudo ./dcdcheck /dev/ttyUSB0
0
[thomas@work scratch]$

If you add water you will get a 1 back instead of a 0.

Monitoring with zabbix is as easy as adding a new usercommand...


UserParameter=watchport.water,sudo /scratch/dcdcheck

Wordpress category: