Permalink
Browse files

Implemented euca-generate-fault inside log.c

    Relates to EUCA-2825
  • Loading branch information...
dmitrii committed Sep 28, 2012
1 parent cb41a1b commit 26de35697d6735e7f3061bebbe6cd117c7a64629
Showing with 86 additions and 7 deletions.
  1. +8 −4 util/Makefile
  2. +78 −3 util/fault.c
View
@@ -64,7 +64,7 @@ include ../Makedefs
#DEBUGS = -DDEBUG # -DDEBUG1
CC += -g
-all: utf8.o log.o config.o fault.o misc.o wc.o hash.o data.o sensor.o euca_auth.o euca_axis.o ipc.o windows-bundle.o euca_rootwrap euca_mountwrap
+all: utf8.o log.o config.o fault.o misc.o wc.o hash.o data.o sensor.o euca_auth.o euca_axis.o ipc.o windows-bundle.o euca_rootwrap euca_mountwrap euca-generate-fault
build: all
@@ -84,7 +84,10 @@ test_wc: wc.c misc.o log.o ../storage/diskutil.o ipc.o
$(CC) $(CFLAGS) $(INCLUDES) $(DEBUGS) -D_UNIT_TEST -o test_wc wc.c misc.o log.o ../storage/diskutil.o ipc.o $(LIBS) $(LDFLAGS)
test_fault: fault.c misc.o log.o wc.o ../storage/diskutil.o ipc.o utf8.o
- $(CC) $(CFLAGS) $(INCLUDES) `xslt-config --cflags` $(DEBUGS) -D_UNIT_TEST -o test_fault fault.c misc.o log.o wc.o ../storage/diskutil.o ipc.o utf8.o $(LIBS) $(LDFLAGS)
+ $(CC) $(CFLAGS) $(INCLUDES) `xslt-config --cflags` $(DEBUGS) -D_UNIT_TEST -o test_fault fault.c misc.o log.o wc.o ../storage/diskutil.o ipc.o utf8.o -lpthread -lxml2 $(LDFLAGS)
+
+euca-generate-fault: fault.c misc.o log.o wc.o ../storage/diskutil.o ipc.o utf8.o
+ $(CC) $(CFLAGS) $(INCLUDES) `xslt-config --cflags` $(DEBUGS) -DEUCA_GENERATE_FAULT -o euca-generate-fault fault.c misc.o log.o wc.o ../storage/diskutil.o ipc.o utf8.o -lpthread -lxml2 $(LDFLAGS)
test_sensor: sensor.c sensor.h misc.o log.o ipc.o ../storage/diskutil.o
$(CC) $(CFLAGS) $(INCLUDES) $(DEBUG) -D_UNIT_TEST -o test_sensor sensor.c misc.o log.o ../storage/diskutil.o ipc.o $(LIBS) $(LDFLAGS)
@@ -96,14 +99,15 @@ test_sensor: sensor.c sensor.h misc.o log.o ipc.o ../storage/diskutil.o
$(CC) -c -std=gnu99 $(CFLAGS) $(INCLUDES) $(DEBUGS) `xslt-config --cflags` $<
clean:
- rm -rf *~ *.o test test_fault test_misc test_wc euca_rootwrap euca_mountwrap test_sensor
+ rm -rf *~ *.o test test_fault euca-generate-fault test_misc test_wc euca_rootwrap euca_mountwrap test_sensor
distclean:
rm -rf eucalyptus-config.h
-install:
+install: all
$(INSTALL) -m 0755 euca_rootwrap $(DESTDIR)$(usrdir)/lib/eucalyptus/
$(INSTALL) -m 0755 euca_mountwrap $(DESTDIR)$(usrdir)/lib/eucalyptus/
+ $(INSTALL) -m 0755 euca-generate-fault $(DESTDIR)$(usrdir)/sbin/
$(INSTALL) -d $(DESTDIR)$(usrdir)/share/eucalyptus/faults/en_US/
$(INSTALL) -m 0644 faults/en_US/common.xml $(DESTDIR)$(usrdir)/share/eucalyptus/faults/en_US/
$(INSTALL) -m 0644 -v faults/en_US/????.xml $(DESTDIR)$(usrdir)/share/eucalyptus/faults/en_US/
View
@@ -37,6 +37,7 @@
#include <unistd.h>
#include <wchar.h>
#include <sys/stat.h>
+#include <ctype.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
@@ -62,6 +63,7 @@
labels */
#define LOCALIZED_TAG "localized"
#define MESSAGE_TAG "message"
+#define STANDARD_FILESTREAM stderr
/*
* This is the order of priority (highest to lowest) for fault messages
@@ -481,7 +483,7 @@ get_eucafault (const char *id, const xmlDoc *doc)
* uses that for building up the path.
*
* Returns TRUE if fault logfile successfully opened, FALSE otherwise
- * (meaning logging is to stderr).
+ * (meaning logging is to console).
*/
static boolean
@@ -491,6 +493,9 @@ initialize_faultlog (const char *fileprefix)
char *fileprefix_i;
if (fileprefix == NULL) {
+ faultlog = STANDARD_FILESTREAM;
+ return TRUE;
+ } else if (strlen (fileprefix) == 0) {
// FIXME: program_infocation_short_name is a GNU'ism and is not
// portable--should wrap with an autoconf check.
snprintf (faultlogpath, PATH_MAX, EUCALYPTUS_LOG_DIR "/%s"
@@ -512,8 +517,8 @@ initialize_faultlog (const char *fileprefix)
if (faultlog == NULL) {
logprintfl (EUCAERROR, "Cannot open fault log file %s: %s\n",
faultlogpath, strerror (errno));
- logprintfl (EUCAERROR, "Logging faults to stderr...\n");
- faultlog = stderr;
+ logprintfl (EUCAERROR, "Logging faults to the console...\n");
+ faultlog = STANDARD_FILESTREAM;
return FALSE;
} else {
return TRUE;
@@ -1006,3 +1011,73 @@ main (int argc, char **argv)
return 0;
}
#endif // _UNIT_TEST
+
+#ifdef EUCA_GENERATE_FAULT
+
+static void
+usage (const char *argv0)
+{
+ fprintf (stderr, "Usage: %s [-c component-name] fault-id [param-1 value-1] [param-2 value-1] ...\n", argv0);
+}
+
+int main (int argc, char **argv)
+{
+ log_params_set (EUCAWARN, 0, 0); // set log level
+ log_prefix_set ("L "); // only print log level
+ log_file_set (NULL); // log output goes to STANDARD_FILESTREAM
+
+ char * component = NULL;
+ int opt;
+ while ((opt = getopt (argc, argv, "c:")) != -1) {
+ switch (opt) {
+ case 'c':
+ component = optarg;
+ break;
+ case 'h':
+ default:
+ usage (argv[0]);
+ return 1;
+ }
+ }
+
+ if (argv[optind]==NULL) {
+ logprintfl (EUCAERROR, "no fault ID is specified (try -h for usage)\n");
+ return 1;
+ }
+ int ndigits = 0;
+ for (char * c = argv[optind]; * c != '\0'; c++, ndigits++) {
+ if ( ! isdigit (* c)) {
+ logprintfl (EUCAERROR, "invalid fault ID (must be a number)\n");
+ return 1;
+ }
+ }
+ if (ndigits < 4) {
+ logprintfl (EUCAERROR, "invalid fault ID (must be a 4-digit number)\n");
+ return 1;
+ }
+ int nfaults = init_eucafaults (component);
+ if (nfaults < 1) {
+ logprintfl (EUCAERROR, "failed to locate fault information (is $EUCALYPTUS set?)\n");
+ return 1;
+ }
+
+ // place variable-and-value pairs from command line into the map
+ char_map ** m = NULL;
+ for (int opt = optind + 1; opt < argc; opt++) {
+ logprintfl (EUCADEBUG, "argv[opt]: %s\n", argv[opt]);
+ if ((opt - optind + 1) % 2) {
+ logprintfl (EUCADEBUG, "...now have two, calling c_varmap_alloc()\n");
+ m = c_varmap_alloc (m, argv[opt - 1], argv[opt]);
+ }
+ }
+
+ int ret = 0;
+ if (log_eucafault_map (argv[optind], (const char_map **)m) == FALSE) {
+ ret = 1;
+ }
+ if (m)
+ c_varmap_free (m);
+
+ return ret;
+}
+#endif // EUCA_GENERATE_FAULT

0 comments on commit 26de356

Please sign in to comment.