source: Evidence/Alarm.cc@ 171

Last change on this file since 171 was 159, checked in by ogrimm, 15 years ago
Change in history handling
File size: 4.5 KB
Line 
1/********************************************************************\
2
3 Alarm handler of the Evidence Control System
4
5 - Checks periodically if all required servers are up
6 (later it should try to start them if not)
7 - Listens to the 'Status' service of each server.
8 - A text describing the state of all servers is published as DIM service.
9 The states are described in StateString[].
10 - A master alarm (indicating most severe of individual alarms) is published.
11
12 Oliver Grimm, January 2010
13
14\********************************************************************/
15
16#define SERVER_NAME "Alarm"
17#include "Evidence.h"
18
19#define SUMMARYSIZE 10000 // Bytes for alarm summary text
20
21const char* StateString[] = {"OK", "WARN", "ERROR", "FATAL", "UNAVAILABLE"};
22
23//
24// Data handling class
25//
26class AlarmHandler : public DimClient, public EvidenceServer {
27
28 DimStampedInfo **StatusService;
29
30 void infoHandler();
31
32 public:
33 AlarmHandler();
34 ~AlarmHandler();
35
36 DimService *Summary, *Master;
37
38 char *AlarmSummary;
39 int MasterAlarm;
40 int *State;
41 char **Server;
42 unsigned int NumServers;
43 char *ServerList;
44
45 void UpdateAlarmSummary();
46};
47
48// Constructor
49AlarmHandler::AlarmHandler(): EvidenceServer(SERVER_NAME) {
50
51 AlarmSummary = new char [SUMMARYSIZE];
52 MasterAlarm = 0;
53
54 char *ServerNames = GetConfig(SERVER_NAME " servers");
55
56 // Create DIM services
57 Summary = new DimService(SERVER_NAME"/Summary", AlarmSummary);
58 Master = new DimService(SERVER_NAME"/MasterAlarm", MasterAlarm);
59
60 // Copy original list of servers to observe
61 ServerList = new char [strlen(ServerNames)+1];
62 strcpy(ServerList, ServerNames);
63
64 // Extract DIM servers to observe
65 Server = new char* [strlen(ServerNames)];
66 NumServers = 0;
67 char *NextToken = strtok(ServerNames, " \t");
68 while (NextToken != NULL) {
69 Server[NumServers++] = NextToken; // Subscribe with handler
70 NextToken = strtok(NULL, " \t");
71 }
72
73 // Subscribe with handler to 'Status' service of all servers
74 StatusService = new DimStampedInfo* [NumServers];
75 State = new int [NumServers];
76
77 for (int i=0; i<NumServers; i++) {
78 char *Buffer = new char [strlen(Server[i])+10];
79 strcpy(Buffer, Server[i]);
80 strcat(Buffer, "/Status");
81 StatusService[i] = new DimStampedInfo(Buffer, NO_LINK, this);
82 delete[] Buffer;
83
84 State[i] = 0;
85 }
86}
87
88// Destructor
89AlarmHandler::~AlarmHandler() {
90
91 for (int i=0; i<NumServers; i++) delete StatusService[i];
92 delete[] StatusService;
93 delete Master;
94 delete Summary;
95 delete[] State;
96 delete[] Server;
97 delete[] ServerList;
98 delete[] AlarmSummary;
99}
100
101// Print messages of status changes to screen and update status string
102void AlarmHandler::infoHandler() {
103
104 // Identify status service
105 for (int i=0; i<NumServers; i++) if (getInfo() == StatusService[i]) {
106
107 // Ignore DIS_DNS (has no status service)
108 if (strcmp(getInfo()->getName(),"DIS_DNS/Status") == 0) return;
109
110 // Update State: unavailable or current severity of status
111 if (!ServiceOK(getInfo())) State[i] = 4;
112 else {
113 State[i] = *(getInfo()->getString()+getInfo()->getSize()-1);
114
115 // Print message
116 time_t RawTime = getInfo()->getTimestamp();
117 struct tm *TM = localtime(&RawTime);
118 printf("%s (%02d:%02d:%02d): %s\n", getInfo()->getName(), TM->tm_hour,
119 TM->tm_min, TM->tm_sec, getInfo()->getString());
120 }
121 UpdateAlarmSummary();
122 }
123}
124
125
126// Update alarm status summary
127void AlarmHandler::UpdateAlarmSummary() {
128
129 int Offset = 0;
130 MasterAlarm = 0;
131
132 for (int i=0; i<NumServers; i++) {
133 snprintf(AlarmSummary+Offset, SUMMARYSIZE-Offset, "%s: %s (%d)\n", Server[i], State[i]<=4 ? StateString[State[i]] : "unknown", State[i]);
134 Offset += strlen(AlarmSummary+Offset);
135 if (State[i] > MasterAlarm) MasterAlarm = State[i];
136 }
137 Summary->updateService();
138 Master->updateService();
139}
140
141//
142// Main program
143//
144int main() {
145
146 DimBrowser Browser;
147 char *ServerName, *Node;
148 bool Exists;
149
150 // Static declaration ensures calling of destructor by exit()
151 static AlarmHandler Alarm;
152
153 // Request configuration data
154 unsigned int Period = atoi(Alarm.GetConfig(SERVER_NAME " period"));
155
156 // Check periodically if servers are up
157 while(!Alarm.ExitRequest) {
158 for (int i=0; i<Alarm.NumServers; i++) {
159 Exists = false;
160 Browser.getServers();
161 while (Browser.getNextServer(ServerName, Node) == 1) {
162 if (strcmp(ServerName, Alarm.Server[i]) == 0) Exists = true;
163 }
164 if (!Exists) Alarm.State[i] = 4;
165 }
166
167 Alarm.UpdateAlarmSummary();
168 sleep(Period);
169 }
170}
Note: See TracBrowser for help on using the repository browser.