1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
|
/*----------------------------------------------------------------------------*/
/* Xymon monitor library. */
/* */
/* This file contains code to calculate availability percentages and do */
/* SLA calculations. */
/* */
/* Copyright (C) 2002-2011 Henrik Storner <henrik@storner.dk> */
/* */
/* This program is released under the GNU General Public License (GPL), */
/* version 2. See the file "COPYING" for details. */
/* */
/*----------------------------------------------------------------------------*/
static char rcsid[] = "$Id: availability.c 8081 2019-08-27 18:50:28Z jccleaver $";
#include <limits.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include "libxymon.h"
typedef struct {
int dow;
time_t start, end;
} reptime_t;
static reptime_t reptimes[10];
static int reptimecnt = 0;
replog_t *reploghead = NULL;
/* TODO: This logic differs from elsewhere below */
char *durationstr(time_t duration)
{
static char dur[100];
char dhelp[100];
if (duration <= 0) {
strncpy(dur, "none", sizeof(dur));
}
else {
dur[0] = '\0';
if (duration > 86400) {
snprintf(dhelp, 100, "%u days ", (unsigned int)(duration / 86400));
duration %= 86400;
strncpy(dur, dhelp, 100);
}
snprintf(dhelp, 100, "%u:%02u:%02u", (unsigned int)(duration / 3600),
(unsigned int)((duration % 3600) / 60), (unsigned int)(duration % 60));
strncat(dur, dhelp, (100 - strlen(dur)));
}
return dur;
}
static time_t secs(int hour, int minute, int sec)
{
return (hour*3600 + minute*60 + sec);
}
static void build_reportspecs(char *reporttime)
{
/* Timespec: W:HHMM:HHMM */
char *spec, *timespec;
int dow, start, end;
int found;
reptimecnt = 0;
spec = strchr(reporttime, '=');
timespec = strdup(spec ? (spec+1) : reporttime);
spec = strtok(timespec, ",");
while (spec) {
if (*spec == '*') {
dow = -1;
found = sscanf(spec+1, ":%d:%d", &start, &end);
}
else if ((*spec == 'W') || (*spec == 'w')) {
dow = -2;
found = sscanf(spec+1, ":%d:%d", &start, &end);
}
else {
found = sscanf(spec, "%d:%d:%d", &dow, &start, &end);
}
if (found < 2) {
errprintf("build_reportspecs: Found too few items in %s\n", spec);
}
reptimes[reptimecnt].dow = dow;
reptimes[reptimecnt].start = secs((start / 100), (start % 100), 0);
reptimes[reptimecnt].end = secs((end / 100), (end % 100), 0);
reptimecnt++;
spec = strtok(NULL, ",");
}
xfree(timespec);
}
static unsigned long reportduration_oneday(int eventdow, time_t eventstart, time_t eventend)
{
int i;
unsigned long result = 0;
for (i=0; (i<reptimecnt); i++) {
if ((reptimes[i].dow == eventdow) || (reptimes[i].dow == -1) || ((reptimes[i].dow == -2) && (eventdow >= 1) && (eventdow <= 5)) ) {
if ((reptimes[i].start > eventend) || (reptimes[i].end < eventstart)) {
/* Outside our window */
}
else {
time_t winstart, winend;
winstart = ((eventstart < reptimes[i].start) ? reptimes[i].start : eventstart);
winend = ((eventend > reptimes[i].end) ? reptimes[i].end : eventend);
result += (winend - winstart);
}
}
}
return result;
}
static unsigned long reportingduration(time_t eventstart, time_t eventduration)
{
struct tm start, end;
time_t eventend;
unsigned long result;
memcpy(&start, localtime(&eventstart), sizeof(start));
eventend = eventstart + eventduration;
memcpy(&end, localtime(&eventend), sizeof(end));
if ((start.tm_mday == end.tm_mday) && (start.tm_mon == end.tm_mon) && (start.tm_year == end.tm_year))
result = reportduration_oneday(start.tm_wday, secs(start.tm_hour, start.tm_min, start.tm_sec), secs(end.tm_hour, end.tm_min, end.tm_sec));
else {
int fulldays = (eventduration - (86400-secs(start.tm_hour, start.tm_min, start.tm_sec))) / 86400;
int curdow = (start.tm_wday == 6) ? 0 : (start.tm_wday+1);
result = reportduration_oneday(start.tm_wday, secs(start.tm_hour, start.tm_min, start.tm_sec), 86400);
while (fulldays) {
result += reportduration_oneday(curdow, 0, 86400);
curdow = (curdow == 6) ? 0 : (curdow+1);
fulldays--;
}
result += reportduration_oneday(curdow, 0, secs(end.tm_hour, end.tm_min, end.tm_sec));
}
return result;
}
static char *parse_histlogfile(char *hostname, char *servicename, char *timespec)
{
char cause[MAX_LINE_LEN];
char fn[PATH_MAX];
char *p;
FILE *fd;
char l[MAX_LINE_LEN];
int causefull = 0;
cause[0] = '\0';
snprintf(fn, PATH_MAX, "%s/%s", xgetenv("XYMONHISTLOGS"), commafy(hostname));
for (p = strrchr(fn, '/'); (*p); p++) if (*p == ',') *p = '_';
snprintf(p, (PATH_MAX - (p - fn)), "/%s/%s", servicename, timespec);
dbgprintf("Looking at history logfile %s\n", fn);
fd = fopen(fn, "r");
if (fd != NULL) {
while (!causefull && fgets(l, MAX_LINE_LEN, fd)) {
p = strchr(l, '\n'); if (p) *p = '\0';
if ((l[0] == '&') && (strncmp(l, "&green", 6) != 0)) {
p = skipwhitespace(skipword(l));
if ((strlen(cause) + strlen(p) + strlen("<BR>\n") + 1) < MAX_LINE_LEN) {
strncat(cause, p, (MAX_LINE_LEN - strlen(cause)));
strncat(cause, "<BR>\n", (MAX_LINE_LEN - strlen(cause)));
}
else causefull = 1;
}
}
#if 1
if (strlen(cause) == 0) {
strncpy(cause, "See detailed log", MAX_LINE_LEN);
}
#else
/* What is this code supposed to do ? The sscanf seemingly never succeeds */
/* storner, 2006-06-02 */
if (strlen(cause) == 0) {
int offset;
rewind(fd);
if (fgets(l, MAX_LINE_LEN, fd)) {
p = strchr(l, '\n'); if (p) *p = '\0';
if (sscanf(l, "%*s %*s %*s %*s %*s %*s %*s %n", &offset) == 1) {
strncpy(cause, l+offset, MAX_LINE_LEN);
}
else {
errprintf("Scan of file %s failed, l='%s'\n", fn, l);
}
cause[MAX_LINE_LEN-1] = '\0';
}
}
#endif
if (causefull) {
cause[MAX_LINE_LEN - strlen(" [Truncated]") - 1] = '\0';
strncat(cause, " [Truncated]", (MAX_LINE_LEN - strlen(cause)));
}
fclose(fd);
}
else {
strncpy(cause, "No historical status available", MAX_LINE_LEN);
}
return strdup(cause);
}
static char *get_historyline(char *buf, int bufsize, FILE *fd, int *err,
char *colstr, unsigned int *start, unsigned int *duration, int *scanres)
{
int ok;
do {
ok = 1;
if (fgets(buf, bufsize, fd) == NULL) {
return NULL;
}
if (strlen(buf) < 25) {
ok = 0;
*err += 1;
dbgprintf("Bad history line (short): %s\n", buf);
continue;
}
*scanres = sscanf(buf+25, "%s %u %u", colstr, start, duration);
if (*scanres < 2) {
ok = 0;
*err += 1;
dbgprintf("Bad history line (missing items): %s\n", buf);
continue;
}
if (parse_color(colstr) == -1) {
ok = 0;
*err += 1;
dbgprintf("Bad history line (bad color string): %s\n", buf);
continue;
}
} while (!ok);
return buf;
}
static int scan_historyfile(FILE *fd, time_t fromtime, time_t totime,
char *buf, size_t bufsize,
time_t *starttime, time_t *duration, char *colstr, unsigned int colstr_buflen)
{
time_t start, dur;
unsigned int uistart, uidur;
int scanres;
int err = 0;
/*
* Format of history entries:
* asctime-stamp newcolor starttime [duration]
*/
/* Is start of history after our report-end time ? */
rewind(fd);
if (!get_historyline(buf, bufsize, fd, &err, colstr, &uistart, &uidur, &scanres)) {
*starttime = getcurrenttime(NULL);
*duration = 0;
strncpy(colstr, "clear", colstr_buflen);
return err;
}
if (scanres == 2) uidur = getcurrenttime(NULL)-uistart;
start = uistart; dur = uidur;
if (start > totime) {
*starttime = start;
*duration = dur;
strncpy(colstr, "clear", colstr_buflen);
return 0;
}
/* First, do a quick scan through the file to find the approximate position where we should start */
while ((start+dur) < fromtime) {
if (get_historyline(buf, bufsize, fd, &err, colstr, &uistart, &uidur, &scanres)) {
start = uistart; dur = uidur;
if (scanres == 2) dur = getcurrenttime(NULL) - start;
if (scanres >= 2) {
dbgprintf("Skipped to entry starting %lu\n", start);
if ((start + dur) < fromtime) {
fseeko(fd, 2048, SEEK_CUR);
if (!fgets(buf, bufsize, fd)) {}; /* Skip partial line */
}
}
}
else {
start = getcurrenttime(NULL);
dur = 0;
}
};
/* We know the start position of the logfile is between current pos and (current-~2048 bytes) */
if (ftello(fd) < 2300)
rewind(fd);
else {
fseeko(fd, -2300, SEEK_CUR);
if (!fgets(buf, bufsize, fd)) {}; /* Skip partial line */
}
/* Read one line at a time until we hit start of our report period */
do {
if (get_historyline(buf, bufsize, fd, &err, colstr, &uistart, &uidur, &scanres)) {
start = uistart; dur = uidur;
if (scanres == 2) dur = getcurrenttime(NULL) - start;
dbgprintf("Got entry starting %lu lasting %lu\n", start, dur);
}
else {
start = getcurrenttime(NULL);
dur = 0;
}
} while ((start+dur) < fromtime);
dbgprintf("Reporting starts with this entry: %s\n", buf);
*starttime = start;
*duration = dur;
return err;
}
static char *timename(char *timestring)
{
static char timespec[26];
char *timecopy;
char *tokens[5];
int i;
/* Compute the timespec string used as the name of the historical logfile */
*timespec = '\0';
timecopy = strdup(timestring);
tokens[0] = tokens[1] = tokens[2] = tokens[3] = tokens[4] = NULL;
tokens[0] = strtok(timecopy, " "); i = 0;
while (tokens[i] && (i < 4)) { i++; tokens[i] = strtok(NULL, " "); }
if (tokens[4]) {
/* Got all 5 elements */
snprintf(timespec, 25, "%s_%s_%s_%s_%s",
tokens[0], tokens[1], tokens[2], tokens[3], tokens[4]);
}
else {
errprintf("Bad timespec in history file: %s\n", timestring);
}
xfree(timecopy);
return timespec;
}
int parse_historyfile(FILE *fd, reportinfo_t *repinfo, char *hostname, char *servicename,
time_t fromtime, time_t totime, int for_history,
double warnlevel, double greenlevel, int warnstops, char *reporttime)
{
char l[MAX_LINE_LEN];
time_t starttime, duration;
unsigned int uistart, uidur;
char colstr[MAX_LINE_LEN];
int color, done, i, scanres;
int fileerrors = 0;
repinfo->fstate = "OK";
repinfo->withreport = 0;
repinfo->reportstart = getcurrenttime(NULL);
for (i=0; (i<COL_COUNT); i++) {
repinfo->count[i] = 0;
repinfo->fullduration[i] = 0;
repinfo->fullpct[i] = 0.0;
repinfo->reportduration[i] = 0;
repinfo->reportpct[i] = 0.0;
}
repinfo->fullavailability = 0.0;
repinfo->reportavailability = 0.0;
repinfo->fullstops = 0;
repinfo->reportstops = 0;
if (reporttime) build_reportspecs(reporttime);
/* Sanity check */
if (totime > getcurrenttime(NULL)) totime = getcurrenttime(NULL);
/* If for_history and fromtime is 0, don't do any seeking */
if (!for_history || (fromtime > 0)) {
fileerrors = scan_historyfile(fd, fromtime, totime,
l, MAX_LINE_LEN, &starttime, &duration, colstr, MAX_LINE_LEN);
}
else {
/* Already positioned (probably in a pipe) */
if (get_historyline(l, MAX_LINE_LEN, fd, &fileerrors, colstr, &uistart, &uidur, &scanres)) {
starttime = uistart; duration = uidur;
if (scanres == 2) duration = getcurrenttime(NULL) - starttime;
}
else {
starttime = getcurrenttime(NULL); duration = 0;
strncpy(colstr, "clear", MAX_LINE_LEN);
fileerrors = 1;
}
}
if (starttime > totime) {
repinfo->fullavailability = repinfo->reportavailability = 100.0;
repinfo->fullpct[COL_CLEAR] = repinfo->reportpct[COL_CLEAR] = 100.0;
repinfo->count[COL_CLEAR] = 1;
return COL_CLEAR;
}
/* If event starts before our fromtime, adjust starttime and duration */
if (starttime < fromtime) {
duration -= (fromtime - starttime);
starttime = fromtime;
}
repinfo->reportstart = starttime;
done = 0;
do {
/* If event ends after our reportend, adjust duration */
if ((starttime + duration) > totime) duration = (totime - starttime);
strncat(colstr, " ", (MAX_LINE_LEN - strlen(colstr))); color = parse_color(colstr);
if (color != -1) {
unsigned long sladuration = 0;
dbgprintf("In-range entry starting %lu lasting %lu color %d: %s", starttime, duration, color, l);
repinfo->count[color]++;
repinfo->fullduration[color] += duration;
if (color > COL_YELLOW) repinfo->fullstops++;
if (reporttime) {
sladuration = reportingduration(starttime, duration);
repinfo->reportduration[color] += sladuration;
if ((color > COL_YELLOW) && (sladuration > 0)) repinfo->reportstops++;
}
if (for_history || ((hostname != NULL) && (servicename != NULL))) {
replog_t *newentry;
char *timespec = timename(l);
newentry = (replog_t *) malloc(sizeof(replog_t));
newentry->starttime = starttime;
newentry->duration = duration;
newentry->color = color;
newentry->affectssla = (reporttime && (sladuration > 0));
if (!for_history && timespec && (color != COL_GREEN)) {
newentry->cause = parse_histlogfile(hostname, servicename, timespec);
}
else newentry->cause = "";
newentry->timespec = (timespec ? strdup(timespec): NULL);
newentry->next = reploghead;
reploghead = newentry;
}
}
if ((starttime + duration) < totime) {
if (get_historyline(l, MAX_LINE_LEN, fd, &fileerrors, colstr, &uistart, &uidur, &scanres)) {
starttime = uistart; duration = uidur;
if (scanres == 2) duration = getcurrenttime(NULL) - starttime;
}
else done = 1;
}
else done = 1;
} while (!done);
for (i=0; (i<COL_COUNT); i++) {
dbgprintf("Duration for color %d: %lu\n", i, repinfo->fullduration[i]);
repinfo->fullpct[i] = (100.0*repinfo->fullduration[i] / (totime - repinfo->reportstart));
}
repinfo->fullavailability = 100.0 - repinfo->fullpct[COL_RED];
if (reporttime) {
repinfo->withreport = 1;
duration = repinfo->reportduration[COL_GREEN] +
repinfo->reportduration[COL_YELLOW] +
repinfo->reportduration[COL_RED] +
repinfo->reportduration[COL_CLEAR];
if (duration > 0) {
repinfo->reportpct[COL_GREEN] = (100.0*repinfo->reportduration[COL_GREEN] / duration);
repinfo->reportpct[COL_YELLOW] = (100.0*repinfo->reportduration[COL_YELLOW] / duration);
repinfo->reportpct[COL_RED] = (100.0*repinfo->reportduration[COL_RED] / duration);
repinfo->reportpct[COL_CLEAR] = (100.0*repinfo->reportduration[COL_CLEAR] / duration);
repinfo->reportavailability = 100.0 - repinfo->reportpct[COL_RED] - repinfo->reportpct[COL_CLEAR];
if (repinfo->reportavailability > greenlevel) color = COL_GREEN;
else if (repinfo->reportavailability >= warnlevel) color = COL_YELLOW;
else color = COL_RED;
if ((warnstops >= 0) && (repinfo->reportstops > warnstops)) color = COL_RED;
}
else {
/* Reporting period has no match with REPORTTIME setting */
repinfo->reportpct[COL_CLEAR] = 100.0;
repinfo->reportavailability = 100.0;
color = COL_GREEN;
}
}
else {
if (repinfo->fullavailability > greenlevel) color = COL_GREEN;
else if (repinfo->fullavailability >= warnlevel) color = COL_YELLOW;
else color = COL_RED;
if ((warnstops >= 0) && (repinfo->fullstops > warnstops)) color = COL_RED;
/* Copy the full percentages/durations to the SLA ones */
repinfo->reportavailability = repinfo->fullavailability;
repinfo->reportstops = repinfo->fullstops;
for (i=0; (i<COL_COUNT); i++) {
repinfo->reportduration[i] = repinfo->fullduration[i];
repinfo->reportpct[i] = repinfo->fullpct[i];
}
}
if (fileerrors) repinfo->fstate = "NOTOK";
return color;
}
replog_t *save_replogs(void)
{
replog_t *tmp = reploghead;
reploghead = NULL;
return tmp;
}
void restore_replogs(replog_t *head)
{
reploghead = head;
}
int history_color(FILE *fd, time_t snapshot, time_t *starttime, char **histlogname)
{
char l[MAX_LINE_LEN];
time_t duration;
char colstr[MAX_LINE_LEN];
int color;
char *p;
*histlogname = NULL;
scan_historyfile(fd, snapshot, snapshot, l, MAX_LINE_LEN, starttime, &duration, colstr, MAX_LINE_LEN);
strncat(colstr, " ", (MAX_LINE_LEN - strlen(colstr)));
color = parse_color(colstr);
if ((color == COL_PURPLE) || (color == -1)) {
color = -2;
}
p = timename(l);
if (p) *histlogname = strdup(p);
return color;
}
#ifdef STANDALONE
time_t reportstart, reportend;
double reportgreenlevel = 99.995;
double reportwarnlevel = 98.0;
int warnstops = -1;
int main(int argc, char *argv[])
{
FILE *fd;
reportinfo_t repinfo;
int i, color;
char *p, *hostsvc, *host, *svc;
replog_t *rwalk;
debug=1;
if (argc != 4) {
fprintf(stderr, "Usage: %s HISTFILE STARTTIME ENDTIME\n", argv[0]);
fprintf(stderr, "Start- and end-times are in Unix epoch format - date +%%s\n");
return 1;
}
fd = fopen(argv[1], "r");
if (fd == NULL) { printf("Cannot open %s\n", argv[1]); exit(1); }
reportstart = atol(argv[2]);
reportend = atol(argv[3]);
hostsvc = strdup(argv[1]);
p = strrchr(hostsvc, '.');
*p = '\0'; svc = p+1;
p = strrchr(hostsvc, '/'); host = p+1;
while ((p = strchr(host, ','))) *p = '.';
color = parse_historyfile(fd, &repinfo, host, svc, reportstart, reportend, 0, reportwarnlevel, reportgreenlevel, warnstops, NULL);
for (i=0; (i<COL_COUNT); i++) {
dbgprintf("Color %d: Count=%d, pct=%.2f\n", i, repinfo.count[i], repinfo.fullpct[i]);
}
dbgprintf("Availability: %.2f, color =%d\n", repinfo.fullavailability, color);
dbgprintf("History file status: %s\n", repinfo.fstate);
fclose(fd);
for (rwalk = reploghead; (rwalk); rwalk = rwalk->next) {
char start[MAXDURSIZE];
char end[MAXDURSIZE];
char dur[MAXDURSIZE], dhelp[MAXDURSIZE];
time_t endtime;
time_t duration;
strftime(start, MAXDURSIZE, "%a %b %d %H:%M:%S %Y", localtime(&rwalk->starttime));
endtime = rwalk->starttime + rwalk->duration;
strftime(end, MAXDURSIZE, "%a %b %d %H:%M:%S %Y", localtime(&endtime));
duration = rwalk->duration;
dur[0] = '\0';
if (duration > 86400) {
snprintf(dhelp, MAXDURSIZE, "%lu days ", (duration / 86400));
duration %= 86400;
strncpy(dur, dhelp, MAXDURSIZE);
}
snprintf(dhelp, MAXDURSIZE, "%lu:%02lu:%02lu", duration / 3600, ((duration % 3600) / 60), (duration % 60));
strncat(dur, dhelp, (MAXDURSIZE - strlen(dur)));
dbgprintf("Start: %s, End: %s, Color: %s, Duration: %s, Cause: %s\n",
start, end, colorname(rwalk->color), dur, rwalk->cause);
}
return 0;
}
#endif
|