root/tools/notifyServicelogEvent.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. status2char
  2. event2status
  3. main

   1 /*
   2  * Original copyright 2009 International Business Machines, IBM, Mark Hamzy
   3  * Later changes copyright 2009-2020 the Pacemaker project contributors
   4  *
   5  * The version control history for this file may have further details.
   6  *
   7  * This source code is licensed under the GNU General Public License version 2
   8  * or later (GPLv2+) WITHOUT ANY WARRANTY.
   9  */
  10 
  11 /* gcc -o notifyServicelogEvent `pkg-config --cflags servicelog-1` `pkg-config --libs servicelog-1` notifyServicelogEvent.c
  12 */
  13 
  14 #include <crm_internal.h>
  15 
  16 #include <string.h>
  17 #include <stdio.h>
  18 #include <time.h>
  19 #include <errno.h>
  20 #include <servicelog.h>
  21 #include <syslog.h>
  22 #include <unistd.h>
  23 #include <inttypes.h>  /* U64T ~ PRIu64, U64TS ~ SCNu64 */
  24 
  25 #ifndef PCMK__CONFIG_H
  26 #  define PCMK__CONFIG_H
  27 #  include <config.h>
  28 #endif
  29 
  30 #include <crm/common/xml.h>
  31 #include <crm/common/util.h>
  32 #include <crm/common/attrd_internal.h>
  33 
  34 typedef enum { STATUS_GREEN = 1, STATUS_YELLOW, STATUS_RED } STATUS;
  35 
  36 const char *status2char(STATUS status);
  37 STATUS event2status(struct sl_event *event);
  38 
  39 const char *
  40 status2char(STATUS status)
     /* [previous][next][first][last][top][bottom][index][help] */
  41 {
  42     switch (status) {
  43         default:
  44         case STATUS_GREEN:
  45             return "green";
  46         case STATUS_YELLOW:
  47             return "yellow";
  48         case STATUS_RED:
  49             return "red";
  50     }
  51 }
  52 
  53 STATUS
  54 event2status(struct sl_event * event)
     /* [previous][next][first][last][top][bottom][index][help] */
  55 {
  56     STATUS status = STATUS_GREEN;
  57 
  58     crm_debug("Severity = %d, Disposition = %d", event->severity, event->disposition);
  59 
  60     /* @TBD */
  61     if (event->severity == SL_SEV_WARNING) {
  62         status = STATUS_YELLOW;
  63     }
  64 
  65     if (event->disposition == SL_DISP_UNRECOVERABLE) {
  66         status = STATUS_RED;
  67     }
  68 
  69     return status;
  70 }
  71 
  72 static pcmk__cli_option_t long_options[] = {
  73     // long option, argument type, storage, short option, description, flags
  74     {
  75         "help", no_argument, NULL, '?',
  76         "\tThis text", pcmk__option_default
  77     },
  78     {
  79         "version", no_argument, NULL, '$',
  80         "\tVersion information", pcmk__option_default
  81     },
  82     {
  83         "-spacer-", no_argument, NULL, '-',
  84         "\nUsage: notifyServicelogEvent event_id", pcmk__option_paragraph
  85     },
  86     {
  87         "-spacer-", no_argument, NULL, '-',
  88         "Where event_id is a unique unsigned event identifier which is "
  89             "then passed into servicelog",
  90         pcmk__option_paragraph
  91     },
  92     { 0, 0, 0, 0 }
  93 };
  94 
  95 int
  96 main(int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  97 {
  98     int argerr = 0;
  99     int flag;
 100     int index = 0;
 101     int rc = 0;
 102     servicelog *slog = NULL;
 103     struct sl_event *event = NULL;
 104     uint64_t event_id = 0;
 105 
 106     crm_log_cli_init("notifyServicelogEvent");
 107     pcmk__set_cli_options(NULL, "<event_id>", long_options,
 108                           "handle events written to servicelog database");
 109 
 110     if (argc < 2) {
 111         argerr++;
 112     }
 113 
 114     while (1) {
 115         flag = pcmk__next_cli_option(argc, argv, &index, NULL);
 116         if (flag == -1)
 117             break;
 118 
 119         switch (flag) {
 120             case '?':
 121             case '$':
 122                 pcmk__cli_help(flag, CRM_EX_OK);
 123                 break;
 124             default:
 125                 ++argerr;
 126                 break;
 127         }
 128     }
 129 
 130     if (argc - optind != 1) {
 131         ++argerr;
 132     }
 133 
 134     if (argerr) {
 135         pcmk__cli_help('?', CRM_EX_USAGE);
 136     }
 137 
 138     openlog("notifyServicelogEvent", LOG_NDELAY, LOG_USER);
 139 
 140     if (sscanf(argv[optind], "%" U64TS, &event_id) != 1) {
 141         crm_err("Error: could not read event_id from args!");
 142 
 143         rc = 1;
 144         goto cleanup;
 145     }
 146 
 147     if (event_id == 0) {
 148         crm_err("Error: event_id is 0!");
 149 
 150         rc = 1;
 151         goto cleanup;
 152     }
 153 
 154     rc = servicelog_open(&slog, 0);     /* flags is one of SL_FLAG_xxx */
 155 
 156     if (!slog) {
 157         crm_err("Error: servicelog_open failed, rc = %d", rc);
 158 
 159         rc = 1;
 160         goto cleanup;
 161     }
 162 
 163     if (slog) {
 164         rc = servicelog_event_get(slog, event_id, &event);
 165     }
 166 
 167     if (rc == 0) {
 168         STATUS status = STATUS_GREEN;
 169         const char *health_component = "#health-ipmi";
 170         const char *health_status = NULL;
 171 
 172         crm_debug("Event id = %" U64T ", Log timestamp = %s, Event timestamp = %s",
 173                   event_id, ctime(&(event->time_logged)), ctime(&(event->time_event)));
 174 
 175         status = event2status(event);
 176 
 177         health_status = status2char(status);
 178 
 179         if (health_status) {
 180             int attrd_rc;
 181 
 182             // @TODO pass pcmk__node_attr_remote when appropriate
 183             attrd_rc = pcmk__node_attr_request(NULL, 'v', NULL,
 184                                                health_component, health_status,
 185                                                NULL, NULL, NULL, NULL,
 186                                                pcmk__node_attr_none);
 187             crm_debug("Updating attribute ('%s', '%s') = %d",
 188                       health_component, health_status, attrd_rc);
 189         } else {
 190             crm_err("Error: status2char failed, status = %d", status);
 191             rc = 1;
 192         }
 193     } else {
 194         crm_err("Error: servicelog_event_get failed, rc = %d", rc);
 195     }
 196 
 197   cleanup:
 198     if (event) {
 199         servicelog_event_free(event);
 200     }
 201 
 202     if (slog) {
 203         servicelog_close(slog);
 204     }
 205 
 206     closelog();
 207 
 208     return rc;
 209 }

/* [previous][next][first][last][top][bottom][index][help] */