File: eventmap2code.pl

package info (click to toggle)
openhpi 2.14.1-1.2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 20,380 kB
  • sloc: ansic: 187,087; cpp: 32,188; sh: 10,415; makefile: 4,467; perl: 1,529
file content (619 lines) | stat: -rwxr-xr-x 18,577 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/perl

##################################################################
# (C) COPYRIGHT IBM Corp 2004, 2006
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This
#  file and program are licensed under a BSD style license. See
#  the Copying file included with the OpenHPI distribution for
#  full licensing terms.
#
#  Author(s):
#       Steve Sherman <stevees@us.ibm.com>
###################################################################

###################################################################
# NOTE!! Parallel make had problems with generated header files. To
# get around this, the code in this script that generates the
# header files has been commented out. Instead the header files have
# been checked into CVS. The HPI_DEF in snmp_bc_event.map have also
# been commented out.
###################################################################

###################################################################
# Script Description:
#
# This script takes raw event information contained in 
# snmp_bc_event.map and generates code that populates the 
# errlog2event_hash table. This can be done in two ways - 
# with C code or with XML code. The C code way is simpler
# but takes more memory. XML is more complex but takes less space.
# 
# The default way is to generate C code and header files to
# populate the hash table. This generates the following files:
#
# el.h - Error log header file with common #defines
# el2event.h - Header file definitions need by generated C file.
# el2event.c - Generated C code that adds events to directly to
#              the errlog2event_hash table.
#
# The second way is to translate events into XML data which is 
# (in a later step and script) turned into source code that
# populates the hash table. This generates the following files:
#
# el.h - Error log header file with common #defines
# el2event.h - Header file definitions need by generated source.
# event.xml - XML formatted events.
#
# If the XML method is used, another script, scripts/text2cstr.pl, 
# needs to be invoked to turn the XML formatted events into source 
# code that populates the hash table.
#
# Script Input:
#
# --debug     (optional)   Turn on debug info.
#                          Default is no.
# --idir      (optional)   Root directory for input file(s).
#                          Default is current directory.
# --mapfile   (optional)   Input file name of the event map file.
#                          Default is snmp_bc_event.map.
# --odir      (optional)   Directory for output file(s).
#                          Default is current directory.
# --xml       (optional)   Generate XML formatted events.
#                          Default is to generate C formatted
#                          events and code.
#
# Exit codes
# - 1 successful
# - 0 error occurred
####################################################################

use strict;
use Getopt::Long;

sub check4dups($$);
#sub print_h_file_header;
#sub print_h_file_ending;
sub print_c_file_header;
sub print_c_file_ending;
sub print_c_file_hash_member($);
sub print_xml_file_header;
sub print_xml_file_ending;
sub print_xml_file_hash_member($);
#sub print_err_hfile_header;
#sub print_err_hfile_ending;

GetOptions(
  "debug"         => \my $debug,
  "idir=s"        => \my $idir,
  "mapfile=s"     => \my $mapfile,
  "odir=s"        => \my $odir,
  "xml"           => \my $xml,
);

##########################
# Set directory/file names
##########################
if ($idir eq "") {
   $idir = `pwd`;
   chomp $idir;
}
if ($mapfile eq "") {
   $mapfile = "snmp_bc_event.map";
}
my $file_map = $idir . "/$mapfile";

if ($odir eq "") {
   $odir = `pwd`;
   chomp $odir;
}

my $oevent_cfile = "el2event.c";
#my $oerror_hfile = "el.h";
#my $oevent_hfile = "el2event.h";

if ($xml) {
    $oevent_cfile = "event.xml";
}

my $file_c = $odir . "/$oevent_cfile";
#my $file_err_h = $odir . "/$oerror_hfile";
#my $file_h = $odir . "/$oevent_hfile";

unlink $file_c;
#unlink $file_h;
#unlink $file_err_h;

############
# Open files
############
open (FILE_MAP, $file_map) or die "$0 Error! Cannot open $file_map. $! Stopped";
open (FILE_C, ">>$file_c") or die "$0 Error! Cannot open file $file_c. $! Stopped";
#open (FILE_H, ">>$file_h") or die "$0 Error! Cannot open file $file_h. $! Stopped";
#open (FILE_ERR_H, ">>$file_err_h") or die "$0 Error! Cannot open file $file_err_h. $! Stopped";

#################################################################
# Parse event map file information into internal perl hash tables
#################################################################
my $err = 0;
my %eventmap = ();
#my %defmap = ();

while ( <FILE_MAP> ) {

    # Skip comments/blank lines
    next if /^\s*\/\// || /^\s*#/ || /^\s*$/;

    my $line = $_;
#    (my $hpidef_event, my $hpidef, my $def) = split/\|/,$line;
    (my $event_name, my $event_hex, my $event_severity,
     my $override_flags, my $event_msg, my $rest) = split/\|/,$line;

#    chomp($def);
    chomp($event_msg);

#    if ($hpidef_event eq "HPI_DEF") {
#	if ($hpidef eq "" || $def eq "" || $defmap{$hpidef} ne "") {
#	    print "******************************************************\n";
#	    print "$0: Error! Definition $hpidef not found or not unique.\n";
#	    print "******************************************************\n\n";
#	    $err = 1;
#	    goto CLEANUP;
#	}
#	$defmap{$hpidef} = $def;
#    }
#    else {
	if ($event_name eq "" || $event_hex eq "" || 
	    $event_severity eq "" || $override_flags eq "" || $event_msg eq "") {
            print "Line is $_\n";
	    print "*************************************************************\n";
	    print "$0: Error! Format for event incomplete for event=$event_name.\n";
	    print "**************************************************************\n\n";
	    $err = 1;
	    goto CLEANUP;
	}
	
        # Put message string into internal hash.
        my $hash_msg = $event_msg;
        $hash_msg =~ s/\"$//;
        check4dups($line, $hash_msg);
#   }
}

##############################
# Create error log header file
##############################
#if (&print_err_hfile_header) { $err = 0; goto CLEANUP; }
#foreach my $d (keys %defmap) {
#    chomp $defmap{$d};
#    print FILE_ERR_H "#define $d $defmap{$d}\n";
#}
#if (&print_err_hfile_ending) { $err = 0; goto CLEANUP; }

#################################################
# Create "Error Log to event" mapping header file
#################################################
#if (&print_h_file_header) { $err = 0; goto CLEANUP; }
#if (&print_h_file_ending) { $err = 0; goto CLEANUP; }

################################################
# Create "Error Log 2 event" mapping source file
################################################
if ($xml) {
    if (&print_xml_file_header) { $err = 0; goto CLEANUP; }
    foreach my $event_message (keys %eventmap) {
	if (&print_xml_file_hash_member($event_message)) { $err = 0; goto CLEANUP; }
    }
    if (&print_xml_file_ending) { $err = 0; goto CLEANUP; }
}
else {
    if (&print_c_file_header) { $err = 0; goto CLEANUP; }
    foreach my $event_message (keys %eventmap) {
	if (&print_c_file_hash_member($event_message)) { $err = 0; goto CLEANUP; }
    }
    if (&print_c_file_ending) { $err = 0; goto CLEANUP; }
}

CLEANUP:
close FILE_MAP;
close FILE_C;
#close FILE_H;
#close FILE_ERR_H;
exit ($err);

##################################################################
# Check for duplicate event messages.
# Add _HPIDUP to both the internal hash_msg name and to the
# external event_msg name. HPI code handles stripping the _HPIDUP
# string from the external names.
# The internal tables thus have hash keys that look like:
#   - msg
#   - msg_HPIDUPx
##################################################################
sub check4dups($$) {

    my ($line, $hash_msg) = @_;
    my ($event_name, $event_hex, $event_severity,
	$override_flags, $event_msg, $rest) = split/\|/,$line;

    chomp($event_msg);

    if ($eventmap{$hash_msg} ne "") {
	if ($debug) {
	    print "$0: Warning! Event=$event_hex; MSG=$event_name not unique.\n";
	}
	# Update number of dups in original entry
	my ($num, $entry) = split/\|/,$eventmap{$hash_msg},2;
	my $dups = $num + 1;
	chomp($entry);
	$eventmap{$hash_msg} = $dups . "|$entry";
	
	# Create unique hash entry and HPI string identifier for each duplicate
	my $hashdup = $hash_msg;
	my $msgdup = $event_msg;
	$hashdup =~ s/\"$//; 
	$msgdup  =~ s/\"$//;
	$hashdup = $hashdup  . "_HPIDUP" . $dups . "\"";
	$msgdup  = $msgdup   . "_HPIDUP" . $dups . "\"";
	$eventmap{$hashdup} =
	    "0|$event_name|$event_hex|$event_severity|$override_flags|$msgdup|$rest";
#	 print ("DUPS msg=$hashdup; 0|$event_name|$event_hex|$event_severity|$override_flags|$msgdup|$rest\n");
    }
    else {
	$eventmap{$hash_msg} =
	    "0|$event_name|$event_hex|$event_severity|$override_flags|$event_msg|$rest";
#	 print ("NonDUPS msg=$hash_msg; 0|$event_name|$event_hex|$event_severity|$override_flags|$event_msg|$rest\n");
    }
    
    return 0;
}

#######################################################
# Print "Error Log to Event" header file's leading text 
#######################################################
#sub print_h_file_header {
#
#    print FILE_H <<EOF;
#/*      -*- linux-c -*-
# *
# * (C) Copyright IBM Corp. 2004
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This
# * file and program are licensed under a BSD style license. See
# * the Copying file included with the OpenHPI distribution for
# * full licensing terms.
# */
#
#/******************************************************************* 
# * WARNING! This file is auto-magically generated by:
# *          $0. 
# *          Do not change this file manually. Update script instead.
# *******************************************************************/
#
##ifndef __EL2EVENT_H
##define __EL2EVENT_H
#
##define HPIDUP_STRING  "_HPIDUP"
#
##define NO_OVR     0x00000000  /* No overrides */
##define OVR_SEV    0x00000001  /* Override Error Log's severity */
##define OVR_RID    0x00000010  /* Override Error Log's source */
##define OVR_EXP    0x00000100  /* Override Error Log's source for expansion cards */
##define OVR_MMACT  0x00001000  /* Override Error Log's source for MM */
##define OVR_MM1    0x00010000  /* Override Error Log's source for MM 1 */
##define OVR_MM2    0x00100000  /* Override Error Log's source for MM 2 */
#
#typedef struct {
#        gchar *event;
#	SaHpiSeverityT event_sev;
#	unsigned int   event_ovr;
#        short          event_dup;
#} ErrLog2EventInfoT;
#
#/* Global \"Error Log to Event\" mapping hash table and usage count */
#extern GHashTable *errlog2event_hash;
#extern unsigned int errlog2event_hash_use_count;
#
#SaErrorT errlog2event_hash_init(struct snmp_bc_hnd *custom_handle);
#SaErrorT errlog2event_hash_free(void);
#EOF
#
#    if ($xml) {
#	print FILE_H <<EOF;
#
#/* XML event code and mapping structures */
#extern char *eventxml;
#
#struct errlog2event_hash_info {
#	GHashTable *hashtable;
#};
#EOF
#    }
#
#    return 0;
#}

#######################################################
# Print "Error Log to Event" header file's leading text 
#######################################################
#sub print_h_file_ending {
#
#    print FILE_H <<EOF;
#
##endif
#EOF
#
#    return 0;
#}

####################################
# Print c file's static leading text 
####################################
sub print_c_file_header {

    print FILE_C <<EOF;
/*      -*- linux-c -*-
 *
 * (C) Copyright IBM Corp. 2004, 2006
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This
 * file and program are licensed under a BSD style license. See
 * the Copying file included with the OpenHPI distribution for
 * full licensing terms.
 */

/******************************************************************* 
 * WARNING! This file is auto-magically generated by:
 *          $0. 
 *          Do not change this file manually. Update script instead.
 *******************************************************************/

#include <glib.h>
#include <string.h>
#include <SaHpi.h>

#include <openhpi.h>
#include <snmp_bc_plugin.h>

GHashTable *errlog2event_hash;

static void free_hash_data(gpointer key, gpointer value, gpointer user_data);

/************************************************************************
 * errlog2event_hash_init:
 * \@custom_handle: Plugin's data pointer.
 * 
 * Initializes the Error Log to event translation hash table.
 *
 * Returns:
 * SA_OK - Normal operation.
 * SA_ERR_HPI_OUT_OF_SPACE - No memory to allocate hash table structures.
 * SA_ERR_HPI_INVALID_PARAMS - \@custom_handle is NULL.
 ************************************************************************/
SaErrorT errlog2event_hash_init(struct snmp_bc_hnd *custom_handle) {

	gchar *key;
/*       gchar *key_exists; */
	ErrLog2EventInfoT *strinfo;

	if (!custom_handle) {
		dbg("Invalid parameter.");
		return(SA_ERR_HPI_INVALID_PARAMS);
	}

	errlog2event_hash = g_hash_table_new(g_str_hash, g_str_equal);
	if (errlog2event_hash == NULL) {
		dbg("No memory.");
		return(SA_ERR_HPI_OUT_OF_SPACE);
	}

EOF
    return 0;
}

#####################################
# Print c file's static trailing text 
#####################################
sub print_c_file_ending {

   print FILE_C <<EOF;

	return(SA_OK);
}

/**********************************************************************
 * errlog2event_hash_free:
 *
 * Frees the Error Log to event translation hash table.
 *
 * Returns:
 * SA_OK - Normal operation.
 **********************************************************************/
SaErrorT errlog2event_hash_free()
{
        g_hash_table_foreach(errlog2event_hash, free_hash_data, NULL);
	g_hash_table_destroy(errlog2event_hash);

	return(SA_OK);
}

static void free_hash_data(gpointer key, gpointer value, gpointer user_data)
{
/*        g_free(key);  keys are string constants - cannot free them */
        g_free(value);
}
EOF

    return 0;
}

#############################
# Print c file's dynamic text 
#############################
sub print_c_file_hash_member($) {
    my ($event_message) = @_;
    
    my ($event_count, $event_name, $event_hex,
	$event_severity, $override_flags, $event_msg, $rest) = 
	    split/\|/,$eventmap{$event_message};

    chomp($event_msg);

    my $event_hex_str = "\"$event_hex\"";
    $event_hex_str =~ s/^\"0x/\"/;

    my $tab = "";

    # Format override flags
    if ($override_flags ne "NO_OVR") { 
	$override_flags =~ s/,/ | /g;
    }
    
    print FILE_C <<EOF;
$tab	key = $event_msg;
$tab	strinfo = g_malloc0(sizeof(ErrLog2EventInfoT));
$tab	if (!strinfo) {
$tab		dbg("No memory. Key=%s.", key);
$tab		errlog2event_hash_free();
$tab		return(SA_ERR_HPI_OUT_OF_SPACE);
$tab	}
$tab	strinfo->event = $event_hex_str; /* $event_name */
$tab	strinfo->event_sev = $event_severity;
$tab	strinfo->event_ovr = $override_flags;
$tab	strinfo->event_dup = $event_count;
$tab	g_hash_table_insert(errlog2event_hash, key, strinfo);
EOF
#$tab
#$tab	key_exists = g_hash_table_lookup(errlog2event_hash, key); 
#$tab	if (!key_exists) {
#$tab		strinfo->event = $event_hex_str; /* $event_name */
#$tab		strinfo->event_sev = $event_severity;
#$tab		strinfo->event_ovr = $override_flags;
#$tab		strinfo->event_dup = $event_count;
#$tab		g_hash_table_insert(errlog2event_hash, key, strinfo);
#$tab	}
#$tab	else {
#$tab		dbg("Error!: Key %s defined twice", key);
#$tab		errlog2event_hash_free();
#$tab		return -1;
#$tab	}
#EOF

    print FILE_C "\n";

    return 0;
}

############################################
# Print error log header file's leading text
############################################
#sub print_err_hfile_header {
#    print FILE_ERR_H <<EOF;
#/*      -*- linux-c -*-
# *
# * (C) Copyright IBM Corp. 2004
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This
# * file and program are licensed under a BSD style license. See
# * the Copying file included with the OpenHPI distribution for
# * full licensing terms.
# */
#
#/******************************************************************* 
# * WARNING! This file is auto-magically generated by:
# *          $0.
# *          Do not change this file manually. Update script instead.
# *******************************************************************/
#
##ifndef __EL_H
##define __EL_H
#
#EOF
#
#return 0;
#
#}

#############################################
# Print error log header file's trailing text
#############################################
#sub print_err_hfile_ending {
#
#    print FILE_ERR_H <<EOF;
#
##endif
#EOF
#
#    return 0;
#}

######################################
# Print XML file's static leading text 
######################################
sub print_xml_file_header {

    print FILE_C <<EOF;
<?xml version="1.0"?>
<!--
 (C) Copyright IBM Corp. 2004

/******************************************************************* 
 * WARNING! This file is auto-magically generated by:
 *          $0. 
 *          Do not change this file manually. Update script instead.
 *******************************************************************/
-->
<events>
EOF

    return 0;
}

#######################################
# Print XML file's static trailing text 
#######################################
sub print_xml_file_ending {

   print FILE_C <<EOF;
</events>
EOF

    return 0;
}

###############################
# Print XML file's dynamic text 
###############################
sub print_xml_file_hash_member($) {
    my ($event_message) = @_;
    
    my ($event_count, $event_name, $event_hex,
	$event_severity, $override_flags, $event_msg, $rest) =
	    split/\|/,$eventmap{$event_message};
    chomp($event_msg);
    my $event_hex_str = "\"$event_hex\"";
    $event_hex_str =~ s/^\"0x/\"/;

    # Format override flags
    if ($override_flags ne "NO_OVR") {
	$override_flags =~ s/,/ | /g;
    }
    
    print FILE_C <<EOF;
<event name="$event_name" hex=$event_hex_str 
       severity="$event_severity" override="$override_flags" dup="$event_count"
       msg=$event_msg />
EOF

    print FILE_C "\n";

    return 0;
}