Skip to content

Commit

Permalink
Add check_loadtrend, to measure changes in CPU load.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Schouten committed Apr 22, 2011
1 parent dc58ba0 commit 0f563ec
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
check_loadtrend
check_sslcert
*.o
8 changes: 5 additions & 3 deletions Makefile
@@ -1,9 +1,11 @@
#CFLAGS=-O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -Werror
LDFLAGS=-lssl
CFLAGS=-O2 -Wall -Wmissing-prototypes -Wstrict-prototypes -Werror

CHECKS=check_sslcert
CHECKS=check_loadtrend check_sslcert

all: $(CHECKS)

check_sslcert: check_sslcert.o
$(CC) $(LDFLAGS) -lssl -o check_sslcert check_sslcert.o

clean:
rm -f *.o *.core *~ $(CHECKS)
102 changes: 102 additions & 0 deletions check_loadtrend.c
@@ -0,0 +1,102 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static void
usage(void)
{
fprintf(stderr,
"usage: check_loadtrend [-m minimum per-cpu load cutoff]\n"
" [-c critical ratio] [-w warning ratio]\n");
exit(1);
}

static double
getdouble(const char *str)
{
double number;
char *ep;

errno = 0;
number = strtod(str, &ep);
if (errno != 0 || *str == '\0' || *ep != '\0') {
fprintf(stderr, "%s: invalid number\n", str);
exit(1);
}
return (number);
}

static void
testratio(double load1, double load2, double ratio,
const char *severity, int code, int loadtime)
{

if (load1 >= ratio * load2) {
printf(
"%s - load1 / load%d = %.2lf / %.2lf = %.2lf >= %.2lf\n",
severity, loadtime, load1, load2, load1 / load2, ratio);
exit(code);
}
}

int
main(int argc, char *argv[])
{
int ch;
double minload = 1.5, critratio = 1.5, warnratio = 1.1;
double ladv[3];
long ncpus;

while ((ch = getopt(argc, argv, "c:m:w:")) != -1) {
switch (ch) {
case 'c':
critratio = getdouble(optarg);
break;
case 'm':
minload = getdouble(optarg);
break;
case 'w':
warnratio = getdouble(optarg);
break;
default:
usage();
}
}
argv += optind;
argc -= optind;
if (argc != 0)
usage();

if (getloadavg(ladv, 3) == -1) {
printf("CRITICAL - Failed to obtain load averages: %s",
strerror(errno));
return (2);
}

ncpus = sysconf(_SC_NPROCESSORS_ONLN);
if (ncpus == -1) {
printf(
"CRITICAL - Failed to obtain the number of online CPUs: %s",
strerror(errno));
return (2);
}

if (ladv[0] < minload * ncpus) {
printf("OK - load1 = %.2lf < %.2lf\n",
ladv[0], minload * ncpus);
return (0);
}

testratio(ladv[0], ladv[1], critratio, "CRITICAL", 2, 5);
testratio(ladv[0], ladv[2], critratio, "CRITICAL", 2, 15);
testratio(ladv[0], ladv[1], warnratio, "WARNING", 1, 5);
testratio(ladv[0], ladv[2], warnratio, "WARNING", 1, 15);

printf("OK - load1 / load5 = %.2lf / %.2lf = %.2lf,"
" load1 / load15 = %.2lf / %.2lf = %.2lf\n",
ladv[0], ladv[1], ladv[0] / ladv[1],
ladv[0], ladv[2], ladv[0] / ladv[2]);
return (0);
}
2 changes: 1 addition & 1 deletion check_sslcert.c
Expand Up @@ -134,7 +134,7 @@ getnum(const char *str)
int
main(int argc, char *argv[])
{
int i, ch, ret;
int ch, ret;

while ((ch = getopt(argc, argv, "c:w:")) != -1) {
switch (ch) {
Expand Down
6 changes: 6 additions & 0 deletions debian/changelog
@@ -1,3 +1,9 @@
nagios-plugins-kumina (1.0-1KUMIRELEASE10) unstable; urgency=low

* Add check_loadtrend.

-- Ed Schouten <ed@kumina.nl> Fri, 22 Apr 2011 13:52:20 +0200

nagios-plugins-kumina (1.0-1KUMIRELEASE9) unstable; urgency=low

* Remove debugging printfs.
Expand Down
2 changes: 1 addition & 1 deletion debian/control
Expand Up @@ -10,4 +10,4 @@ Depends: ${shlibs:Depends}
Description: Nagios plugins provided by Kumina
The Nagios plugins by Kumina package installs additional plugins for
Nagios. Currently it only installs a plugin to check for SSL
certificate validity.
certificate validity and a plugin to measure trends in CPU load.
2 changes: 2 additions & 0 deletions debian/rules
Expand Up @@ -20,6 +20,8 @@ install: build
${NPDIR}/usr/share/doc/nagios-plugins-kumina
install -m 644 debian/copyright \
${NPDIR}/usr/share/doc/nagios-plugins-kumina
install -m 755 check_loadtrend \
${NPDIR}/usr/lib/nagios/plugins
install -m 755 check_sslcert \
${NPDIR}/usr/lib/nagios/plugins
dh_installchangelogs
Expand Down

0 comments on commit 0f563ec

Please sign in to comment.