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  * Copyright (C) 2009 International Business Machines, IBM, Mark Hamzy
   3  *
   4  * This program is free software; you can redistribute it and/or
   5  * modify it under the terms of the GNU General Public
   6  * License as published by the Free Software Foundation; either
   7  * version 2 of the License, or (at your option) any later version.
   8  *
   9  * This software is distributed in the hope that it will be useful,
  10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12  * General Public License for more details.
  13  *
  14  * You should have received a copy of the GNU General Public
  15  * License along with this library; if not, write to the Free Software
  16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  17  */
  18 
  19 /* gcc -o notifyServicelogEvent `pkg-config --cflags servicelog-1` `pkg-config --libs servicelog-1` notifyServicelogEvent.c
  20 */
  21 
  22 #include <string.h>
  23 #include <stdio.h>
  24 #include <time.h>
  25 #include <errno.h>
  26 #include <servicelog.h>
  27 #include <syslog.h>
  28 #include <unistd.h>
  29 #include <config.h>
  30 #include <crm/common/xml.h>
  31 #include <crm/common/util.h>
  32 #include <crm_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 struct crm_option long_options[] = {
  73     /* Top-level Options */
  74     {"help", 0, 0, '?', "\tThis text"},
  75     {"version", 0, 0, '$', "\tVersion information"},
  76     {"-spacer-", 0, 0, '-', "\nUsage: notifyServicelogEvent event_id"},
  77     {"-spacer-", 0, 0, '-',
  78      "\nWhere event_id is unique unsigned event identifier which is then passed into servicelog"},
  79 
  80     {0, 0, 0, 0}
  81 };
  82 
  83 int
  84 main(int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  85 {
  86     int argerr = 0;
  87     int flag;
  88     int index = 0;
  89     int rc = 0;
  90     servicelog *slog = NULL;
  91     struct sl_event *event = NULL;
  92     uint64_t event_id = 0;
  93 
  94     crm_log_init_quiet("notifyServicelogEvent", LOG_INFO, FALSE, TRUE, argc, argv);
  95     crm_set_options(NULL, "event_id ", long_options,
  96                     "Gets called upon events written to servicelog database");
  97 
  98     if (argc < 2) {
  99         argerr++;
 100     }
 101 
 102     while (1) {
 103         flag = crm_get_option(argc, argv, &index);
 104         if (flag == -1)
 105             break;
 106 
 107         switch (flag) {
 108             case '?':
 109             case '$':
 110                 crm_help(flag, 0);
 111                 break;
 112             default:
 113                 ++argerr;
 114                 break;
 115         }
 116     }
 117 
 118     if (argc - optind != 1) {
 119         ++argerr;
 120     }
 121 
 122     if (argerr) {
 123         crm_help('?', 1);
 124     }
 125 
 126     openlog("notifyServicelogEvent", LOG_NDELAY, LOG_USER);
 127 
 128     if (sscanf(argv[optind], U64T, &event_id) != 1) {
 129         crm_err("Error: could not read event_id from args!");
 130 
 131         rc = 1;
 132         goto cleanup;
 133     }
 134 
 135     if (event_id == 0) {
 136         crm_err("Error: event_id is 0!");
 137 
 138         rc = 1;
 139         goto cleanup;
 140     }
 141 
 142     rc = servicelog_open(&slog, 0);     /* flags is one of SL_FLAG_xxx */
 143 
 144     if (!slog) {
 145         crm_err("Error: servicelog_open failed, rc = %d", rc);
 146 
 147         rc = 1;
 148         goto cleanup;
 149     }
 150 
 151     if (slog) {
 152         rc = servicelog_event_get(slog, event_id, &event);
 153     }
 154 
 155     if (rc == 0) {
 156         STATUS status = STATUS_GREEN;
 157         const char *health_component = "#health-ipmi";
 158         const char *health_status = NULL;
 159 
 160         crm_debug("Event id = " U64T ", Log timestamp = %s, Event timestamp = %s",
 161                   event_id, ctime(&(event->time_logged)), ctime(&(event->time_event)));
 162 
 163         status = event2status(event);
 164 
 165         health_status = status2char(status);
 166 
 167         if (health_status) {
 168             gboolean rc;
 169 
 170             /* @TODO pass attrd_opt_remote when appropriate */
 171             rc = (attrd_update_delegate(NULL, 'v', NULL, health_component,
 172                                         health_status, NULL, NULL, NULL, NULL,
 173                                         attrd_opt_none) > 0);
 174             crm_debug("Updating attribute ('%s', '%s') = %d",
 175                       health_component, health_status, rc);
 176         } else {
 177             crm_err("Error: status2char failed, status = %d", status);
 178             rc = 1;
 179         }
 180     } else {
 181         crm_err("Error: servicelog_event_get failed, rc = %d", rc);
 182     }
 183 
 184   cleanup:
 185     if (event) {
 186         servicelog_event_free(event);
 187     }
 188 
 189     if (slog) {
 190         servicelog_close(slog);
 191     }
 192 
 193     closelog();
 194 
 195     return rc;
 196 }

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