User avatar
Skorium
Posts: 8
Joined: Sat Jun 29, 2013 8:46 pm
Location: Dublin, Ireland
Contact: Website

Service(s) Status monitoring with led(s)

Tue May 14, 2019 7:21 pm

Hi guys,

I'm trying to find a clean way to use a led (on/off/blink) to monitor the status of a service, I have spent few hours looking for information but I'm unable to find an answer. I found a couple of things using system() call, but most people doesn't recommend it without offering any alternative so I'm kind of blocked as I will like to do something as clean as possible.

A situation as example:
  • Monitoring SSH service: LED off = service not running / LED on = service running, no active connection / LED blinking = service running, at least one active connection
  • Monitoring OpenVPN service: LED off = service not running / LED on = service running, no active connection / LED blinking = service running, at least one active connection
The Raspberry Pi runs the latest Raspbian Lite (at the date of this post) and ready to use, wiringpi lib installed if needed. I'm fairly advanced user with Linux in general.

Do you have any idea or example to share ? I'm not looking for a complete solution specifically (if you have one I will take it of course without complaining :D ) but I do have some "rusty" programming skills but if I know where to look at I will be happy (doesn't matter the language I can adapt myself, C, C++, Python, Shell... you have it, doesn't matter).

Hope you will have some ideas to share.

Thanks,

S.

Andyroo
Posts: 3384
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Service(s) Status monitoring with led(s)

Tue May 14, 2019 7:40 pm

You could look at packages like Nagios but they would be overkill to say the least :lol:

I would start with https://thepihut.com/collections/raspbe ... -board-pro and the code on https://github.com/ThePiHut/statusboard

Main issue to work out is how to tell the task is still active and not locked up. The process may still be active but is it working?
I’ve used external service that checked websites and they loaded pages, did dummy searches and even ran transactions to a dummy delivery point - you may just be happy to see if things are still running :lol:
Need Pi spray - these things are breeding in my house...

User avatar
Skorium
Posts: 8
Joined: Sat Jun 29, 2013 8:46 pm
Location: Dublin, Ireland
Contact: Website

Re: Service(s) Status monitoring with led(s)

Tue May 14, 2019 8:44 pm

Thanks Andyroo.

Nagios, I haven't think about it. I had done some tests with it on Raspberry Pi in the past to get more familiar with it, as we have one instance Nagios XI at work. Are you aware of any plug-in to interface it with Raspberry Pi GPIO ? If yes, any recommendation ? I will be honest, I haven't search their plug-ins "store" yet.

StatusBoard from ThePiHut seems to be a nice starting point indeed but seems fairly limited too. I will install it to have a better overview than the very few and simple examples available on GitHub. See if I can go a step further but I have little hope about it (getting active connection seems to be another level).

If you have further ideas, I'm willing to read about it/them, anyone ? Not limited to Andyroo who had the privilege to reply 1st and really quickly :)
Last edited by Skorium on Tue May 14, 2019 9:00 pm, edited 1 time in total.

Andyroo
Posts: 3384
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Service(s) Status monitoring with led(s)

Tue May 14, 2019 8:57 pm

There is a specific Pi version of Nagios at https://nemslinux.com/ but I do not know if it has GPIO access modules.

For me, Nagios is a package for workplaces and not my home :lol:
Need Pi spray - these things are breeding in my house...

Andyroo
Posts: 3384
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Service(s) Status monitoring with led(s)

Tue May 14, 2019 9:12 pm

A lot of Linux programs store their process ID (PID) in a .pid file and these can be read as a text file.

If you know the PID number from a file then you can use https://psutil.readthedocs.io/en/latest/#

Code: Select all

import psutil
psutil.pids()
and see if it is in the returned list - if not it’s died!

Code: Select all

psutil.pid_exists(pid)
checks for one task at a time.

If the program does not write out its PID you can use the first bit of https://psutil.readthedocs.io/en/latest ... ss-by-name

Note there are other modules in Python to do this but psutils runs on Mac, Windows and Linux so it’s handy to know about it :D
Need Pi spray - these things are breeding in my house...

User avatar
Skorium
Posts: 8
Joined: Sat Jun 29, 2013 8:46 pm
Location: Dublin, Ireland
Contact: Website

Re: Service(s) Status monitoring with led(s)

Wed May 15, 2019 8:14 pm

psutil interesting one too, thanks Andyroo ( I think I found "The Guy" 8-) )

I also find some guys using popen() in C to get data from other commands. such as netstat to find out how many SSH clients are connected, so can get a count easily. Also can run through popen() "systemctl show -p SubState ssh" to get the current state of the ssh service, with some piping and regex there is way to get some nice data and deal with them accordingly.

I will do my experiments, I will come back with my findings on these. See what seems the better solution at least for me and what I feel confortable with.

S.

User avatar
Skorium
Posts: 8
Joined: Sat Jun 29, 2013 8:46 pm
Location: Dublin, Ireland
Contact: Website

Re: Service(s) Status monitoring with led(s)

Fri May 17, 2019 9:42 pm

So I came up with a small C program to get SSH activity, I share the source below for people who want to do something similar. You will need wiringPi lib installed.

Code: Select all

/* Author      :   Arnaud Nicole
 * Project     :   RPi SSH Status LED
 * License     :   Public Domain
 * Version     :   0.1.0
 * Description :   This program uses LED connected to GPIO to display status of SSH Service.
 */
#include<stdio.h>
#include<string.h>
#include<wiringPi.h>

#define PIN_LED_SSH 24

bool GetStatus_SSH() {
	bool status = false;
	FILE *temp_ssh_status;
	temp_ssh_status = popen("systemctl show -p SubState ssh | sed 's/^SubState=//g'", "r");
	char data[32] = "";
	if (!temp_ssh_status) {
		status = false;
	} else {
		fgets(data, 32, temp_ssh_status);
		if (strncmp(data,"running", 7) == 0) {
			status = true;
		} else if (strncmp(data,"dead", 4) == 0) {
			status = false;
		} else {
			/* This need some testing probably so I print out the status for information */
			printf("SSH Service Status: %s", data);
			status = false;
		}
	}
	fclose(temp_ssh_status);
	return status;
}

bool GetActivity_SSH() {
	bool activity = false;
	FILE *temp_ssh_activity;
	int count = 0;
	char data[32] = ""; // Shouldn't exceed 25 or 26 but taking some margin of errors.
 	/* Get active connection(s) - Get IP(s) can be used to also send the list of IPs by email for example */
	temp_ssh_activity = popen("netstat -atp | grep 'ESTABLISHED.*ssh' | awk '{print $5}' | sed 's/:ssh//'", "r");
	if (temp_ssh_activity) {
		/* Count connection */
		while (fscanf(temp_ssh_activity, "%s", data) != EOF) count++;
		if (count >= 1) {
			activity = true;
		}
	}
	fclose(temp_ssh_activity);
	return activity;
}

int main(int argc, char *argv[]) {
	if (wiringPiSetup() < 0) {
		printf("Unable to Setup WiringPi\r\n");
		return 1;
	}
	/* Initialisation  */
	pinMode (PIN_LED_SSH, OUTPUT);
	digitalWrite(PIN_LED_SSH, LOW);
	while (1) {
		if (GetActivity_SSH()) {
			/* SSH Service is running - Active Connection(s) */
			for (int i = 0; i <= 3; i++) {
				digitalWrite(PIN_LED_SSH, LOW);
				delay(250);
				digitalWrite(PIN_LED_SSH, HIGH);
				delay(250);
			}
		} else if(GetStatus_SSH()) {
			/* SSH Service is running - No Active Connection */
			digitalWrite(PIN_LED_SSH, HIGH);
		} else {
			/* SSH Service is not working... This need some testing probably */
			digitalWrite(PIN_LED_SSH, LOW);
		}
	}
	return 0;
}
If you know how to get the shared code looks better let me know :D

Andyroo
Posts: 3384
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Service(s) Status monitoring with led(s)

Fri May 17, 2019 10:42 pm

I would use github - you can publish the code, keep track of problems, write documentation and stre different versions all free of charge.
Need Pi spray - these things are breeding in my house...

Return to “Advanced users”