This is essentially its first draft, there's a lot to do still, but I wanted to get a bare minimum form going to see how it works in the web interface.
Code: Select all
const char *progname = "check_thing";
const char *copyright = "";
const char *email ="";
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
int process_arguments(int, char**);
struct msg parse_msg (char *);
void print_help(void);
void print_usage(void);
static int wcount = 0;
static int ecount = 0;
static long loffset;
static long moffset;
int verbose = 0;
char *logpath = NULL;
static char *foundlog = "foundlog.txt";
int warthresh; /*If number of warnings surpass this, return CRITICAL*/
int errthresh; /*If number of errors surpass this, return CRITICAL*/
const int ERROR = -1;
const int STATUS_OK, OK = 0;
const int STATUS_WARNING = 1;
const int STATUS_CRITICAL = 2;
const int STATUS_UNKNOWN = 3;
struct msg{
char time[16];
char fac[20];
char prefix[20];
char body[100];
int seconds;
};
int main(int argc, char **argv){
int result = STATUS_UNKNOWN;
struct msg mess;
char line[156];
char *fline;
FILE* thingfile = fopen(foundlog, "a+");
if (process_arguments(argc, argv) == ERROR){
printf("Could not parse arguments\n");
print_usage();
exit(STATUS_UNKNOWN);
}
FILE* logfile = fopen(logpath, "r");
fseek(logfile, loffset, SEEK_SET);
fseek(thingfile, moffset, SEEK_SET);
while (fgets(line, 156, logfile)){
if (strstr((const char *)line, "sshd[")){
/*record line in file*/
fline = strncat(line, "\n", 1);
fputs(fline, thingfile);
mess = parse_msg(fline);
if (!strncmp(mess.body, "Warning:", 8)){
wcount++;
} else {
ecount++;
}
}
}
loffset=ftell(logfile);
moffset=ftell(thingfile);
fclose(logfile);
if (wcount > 0 || ecount > 0){
if (wcount >= warthresh || ecount >= errthresh){
result = STATUS_CRITICAL;
}else {
result = STATUS_WARNING;
}
}else {
result = STATUS_OK;
}
/*First line of output; stored in $SERVICEOUTPUT*/
printf ("status : %s", (result == STATUS_OK) ? ("OK") : ("WARNING"));
printf (", Report : %d uncleared warnings and %d uncleared errors logged in %s\n",
wcount, ecount, logpath);
/*Verbose output; stored in $LONGSERVICEOUTPUT*/
if (verbose){
printf ("Last reports from %s: \n", logpath); /*TODO*/
}
fclose(thingfile);
return result;
}
struct msg parse_msg(char *line){
struct msg mess;
sscanf(line, "%15[a-zA-Z0-9: ]s", mess.time);
mess.seconds = atoi(mess.time+13);
sscanf((line+15), "%s %s %[^\n]s", mess.fac, mess.prefix, mess.body);
return mess;
}
int process_arguments(int argc, char **argv){
int c;
int option = 0;
static struct option longopts[] = {
{"filename", required_argument, 0, 'F'},
{"warning count", required_argument, 0, 'w'},
{"error count", required_argument, 0, 'e'},
{"log", optional_argument, 0, 'l'},
{"help", no_argument, 0, 'h'},
{"verbose", no_argument, 0, 'v'},
{NULL, 0, 0, 0}
};
/*if no arguments were passed*/
if (argc < 2)
return ERROR;
while(1){
c = getopt_long(argc, argv, "+hvF:w:e:", longopts, &option);
if (c==-1 || c == EOF || c ==1)
break;
switch(c) {
case 'h':
print_help();
exit (STATUS_OK);
case 'F':
logpath = optarg;
break;
case 'w':
warthresh = atoi(optarg);
break;
case 'e':
errthresh = atoi(optarg);
break;
case 'v':
verbose++;
break;
default:
print_usage();
exit(STATUS_UNKNOWN);
}
}
/*set mainlogfile to default syslog file if not user-defined*/
if(logpath==NULL){
logpath = "/var/log/messages";
}
return 0;
}
void print_help(void){
printf("print relevant help line here \n");
printf("\n\n");
print_usage();
}
void print_usage(void){
printf("\nUsage:\n");
printf("%s -F <logfile> -w <warn threshold> -e <error threshold> \n", progname);
}
Also, yes, when I execute it to test it, I use root.