- Timestamp:
- 05/19/10 09:08:40 (15 years ago)
- Location:
- Evidence
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
Evidence/Config.cc
r203 r209 105 105 if (pthread_mutex_lock(&Mutex) != 0) State(ERROR, "pthread_mutex_lock() failed in rpcHandler()"); 106 106 107 // Search for config data in list 107 // Search for config data in list (default response is empty string) 108 108 for (int i=0; i<List.size(); i++) { 109 109 if (List[i].Name == getString()) Response = List[i].Data; -
Evidence/DColl.cc
r203 r209 5 5 - DColl subscribes to all services given by the configuration 6 6 server and writes these to the data file at every update. 7 - One data file per day is generated, with roll-over at 13:00 local time. 8 - For each service, it creates a new service with '.hist' appended 9 that contains a history of events kept within a ring buffer. Each 7 - One data file per day is generated. 8 - A history of events is kept within a ring buffer for each service. Each 10 9 entry will be the result of a conversion to double of the text 11 10 written to the data file. Only if the new value has changed by a 12 minimum amout it will be added to the ring buffer. 11 minimum amout it will be added to the ring buffer. The history is 12 available via an rpc call. 13 13 - The history buffers are written to disk at program termination and 14 14 are tired to be read when adding a service. … … 16 16 file specified in the configuration. 17 17 18 Oliver Grimm, December 200918 Oliver Grimm, May 2010 19 19 20 20 \********************************************************************/ … … 22 22 #define SERVER_NAME "DColl" 23 23 24 #define DATE_ROLLOVER 12 // localtime hour after which next date is used25 24 #define MIN_HIST_SIZE 1024 // Minimum history buffer in bytes (> 3*sizeof(int) !) 26 25 … … 44 43 // Class declaration 45 44 // 46 class DataHandler: public Dim Client, public DimBrowser,45 class DataHandler: public DimRpc, public DimClient, public DimBrowser, 47 46 public EvidenceServer { 48 47 49 48 struct Item { 50 49 DimStampedInfo *DataItem; 51 DimService *HistService;52 50 char *Buffer; 53 51 unsigned int HistSize; … … 70 68 int SizeUpdateDelay; 71 69 int TimeForNextFile; 72 70 int RollOver; 71 73 72 int RegExCount; 74 73 regex_t *RegEx; … … 77 76 78 77 void infoHandler(); 78 void rpcHandler(); 79 79 void commandHandler(); 80 80 void AddService(string); 81 81 void RemoveService(string); 82 82 float FileSize(FILE *); 83 83 FILE *OpenHistFile(string, const char *); 84 84 85 public: 85 86 DataHandler(); … … 90 91 // Constructor 91 92 // 92 DataHandler::DataHandler(): EvidenceServer(SERVER_NAME) {93 DataHandler::DataHandler(): DimRpc("ServiceHistory", "C", "C"), EvidenceServer(SERVER_NAME) { 93 94 94 95 // Initialization to prevent freeing unallocated memory … … 108 109 SizeUpdateDelay = atoi(GetConfig("sizeupdate")); 109 110 HistDir = GetConfig("histdir"); 110 111 RollOver = atoi(GetConfig("rollover")); 112 111 113 // Open log file 112 114 char *Logname = GetConfig("logfile"); … … 127 129 DataFilename = new DimService(SERVER_NAME "/CurrentFile", (char *) ""); 128 130 129 // Count how many minimum change valueregular expressions are present131 // Count how many minimum change regular expressions are present 130 132 char *Change = GetConfig("items"); 131 133 RegExCount = 0; … … 137 139 138 140 // Allocate memory for regular expressions, minimum change and history size 139 RegEx = new regex_t [RegExCount];141 RegEx = new regex_t [RegExCount]; 140 142 MinChange = new double [RegExCount]; 141 143 HistSize = new unsigned int [RegExCount]; … … 151 153 char ErrMsg[200]; 152 154 regerror(Ret, &RegEx[i], ErrMsg, sizeof(ErrMsg)); 153 State(FATAL, "Error compiling regular expression '%s' (%s)", Token, ErrMsg); 155 RegExCount--; 156 i--; 157 State(ERROR, "Error compiling regular expression '%s' (%s)", Token, ErrMsg); 154 158 } 155 159 else { … … 179 183 delete[] Filename; 180 184 181 //delete LogSizeService; // These create segmentation faults?!182 //delete DataSizeService;185 delete LogSizeService; 186 delete DataSizeService; 183 187 184 188 // Close files … … 258 262 DataFile = NULL; 259 263 } 260 264 261 265 // Open new data file if necessary 262 266 if (DataFile == NULL) { 263 267 time_t Time = time(NULL); 264 268 struct tm *T = localtime(&Time); 265 269 266 270 // Get time structure with date rollover 267 if (T->tm_hour >= DATE_ROLLOVER) T->tm_mday++;271 if (T->tm_hour >= RollOver) T->tm_mday++; 268 272 if (mktime(T) == -1) State(ERROR, "mktime() failed, check filename"); 269 273 … … 294 298 T->tm_sec = 0; 295 299 T->tm_min = 0; 296 T->tm_hour = DATE_ROLLOVER;300 T->tm_hour = RollOver; 297 301 TimeForNextFile = mktime(T); 298 302 } … … 460 464 461 465 // 466 // Implementation of history buffer distribution 467 // 468 void DataHandler::rpcHandler() { 469 470 // Search for history buffer 471 for (int i=0; i<List.size(); i++) { 472 if (strcmp(List[i].DataItem->getName(), getString()) == 0) { 473 setData((void *) List[i].Buffer, List[i].HistSize); 474 return; 475 } 476 } 477 478 // Default response 479 setData(NULL, 0); 480 } 481 482 483 // 462 484 // Add service to watch list 463 485 // 464 486 void DataHandler::AddService(string Name) { 465 466 // Do not subscribe to history services (otherwise infinite loop)467 if (Name.find(".hist") != string::npos) return;468 487 469 488 // Check if already subscribed to this service … … 492 511 New.Next = 4; 493 512 New.LastValue = DBL_MAX; 494 New.HistService = new DimService ((Name+".hist").c_str(), (char *) "C",495 New.Buffer, New.HistSize);496 513 497 514 // Load history buffer from file if existing 498 string Filename = New.HistService->getName(); 499 for (int j=0; j<Filename.size(); j++) if (Filename[j] == '/') Filename[j] = '_'; 500 FILE *File = fopen((HistDir + "/" + Filename).c_str(), "rb"); 515 FILE *File = OpenHistFile(Name, "rb"); 501 516 if (File != NULL) { 502 // Only load if current buffer size i fequal or larger517 // Only load if current buffer size is equal or larger 503 518 if (FileSize(File) <= New.HistSize*sizeof(char)+sizeof(New.Next) && FileSize(File) != -1) { 504 519 fread(&New.Next, sizeof(New.Next), 1, File); … … 527 542 delete (*E).DataItem; 528 543 529 // Save history buffer (replace '/' by '_') 530 string Name = (*E).HistService->getName(); 531 for (int j=0; j<Name.size(); j++) if (Name[j] == '/') Name[j] = '_'; 532 FILE *File = fopen((HistDir + "/" + Name).c_str(), "wb"); 544 // Save history buffer 545 FILE *File = OpenHistFile(Name, "wb"); 533 546 if (File != NULL) { 534 547 fwrite(&(*E).Next, sizeof((*E).Next), 1, File); … … 538 551 539 552 // Delete history service and free memory 540 delete (*E).HistService;541 553 delete[] (*E).Buffer; 542 554 List.erase(E); … … 558 570 559 571 return (float) FileStatus.st_size/1024; 572 } 573 574 // 575 // Replace all '/' by '_' in string 576 // 577 FILE *DataHandler::OpenHistFile(string Service, const char *Mode) { 578 579 for (int i=0; i<Service.size(); i++) if (Service[i] == '/') Service[i] = '_'; 580 return fopen((HistDir + "/" + Service).c_str(), Mode); 560 581 } 561 582 -
Evidence/Evidence.cc
r199 r209 38 38 // Initialize 39 39 Status = NULL; 40 StdOutText = NULL; 40 41 ExitRequest = false; 41 42 ThisServer = this; … … 60 61 61 62 Status = new DimService((ServerName+"/Status").c_str(), (char *) "C", InitMsg, strlen(InitMsg)+1); 63 StdOut = new DimService((ServerName+"/Textout").c_str(), (char *) ""); 62 64 63 65 start(Name); … … 74 76 } 75 77 delete ModifyInfo; 78 79 delete Status; 80 delete StdOut; 76 81 } 77 82 … … 126 131 // Terminate if message type is fatal 127 132 if (Severity == FATAL) exit(EXIT_FAILURE); 133 } 134 135 // Set text of StdOut service 136 void EvidenceServer::SetStdOut(char *Text) { 137 138 // Copy text to permanent buffer 139 char *Tmp = new char[strlen(Text)+1]; 140 strcpy(Tmp, Text); 141 StdOut->updateService(Tmp); 142 143 // Delete old buffer and save new buffer pointer 144 delete[] StdOutText; 145 StdOutText = Tmp; 128 146 } 129 147 … … 148 166 break; 149 167 } 150 168 151 169 // Make configuration request 152 DimRpcInfo Config((char *) "ConfigRequest", (char *) "");170 DimRpcInfo Config((char *) "ConfigRequest", NO_LINK); 153 171 Config.setData((char *) (ServerName + " " + Item).c_str()); 154 172 char *Result = Config.getString(); 155 173 156 174 // Terminate if not successful 175 if (!EvidenceServer::ServiceOK(&Config)) { 176 State(FATAL, "Configuration server unreachable, can't get '%s'", Item.c_str()); 177 } 178 157 179 if (strlen(Result) == 0) { 158 180 if (Default == NULL) State(FATAL, "Missing configuration data '%s'", Item.c_str()); … … 172 194 strcpy(ConfigList[ItemNo].Value, Result); 173 195 ConfigList[ItemNo].Time = ModifyInfo->LastModifyTime; 174 196 175 197 // Return address to configuration value 176 198 return ConfigList[ItemNo].Value; … … 314 336 return !((Item->getSize() == strlen(NO_LINK)+1) && 315 337 (memcmp(Item->getData(), NO_LINK, Item->getSize()) == 0)); 316 317 } 318 338 } 339 340 bool EvidenceServer::ServiceOK(DimRpcInfo *Item) { 341 342 return !((Item->getSize() == strlen(NO_LINK)+1) && 343 (memcmp(Item->getData(), NO_LINK, Item->getSize()) == 0)); 344 } 319 345 320 346 /////////////////////////// … … 333 359 334 360 // Constructor 335 EvidenceHistory::EvidenceHistory(std::string Name, int Delay): 336 Name(Name+".hist"), 337 Delay(Delay) { 361 EvidenceHistory::EvidenceHistory(std::string Name): Name(Name) { 338 362 339 363 Buffer = NULL; 340 LastUpdate = 0;341 364 } 342 365 … … 350 373 bool EvidenceHistory::GetHistory() { 351 374 352 // Check if last buffer update less than minimum delay in the past 353 if ((Buffer != NULL) && (time(NULL)-LastUpdate < Delay)) { 354 Offset = *(int *) Buffer; 355 return true; 356 } 357 LastUpdate = time(NULL); 358 359 // Check if service available 360 DimCurrentInfo Info(Name.c_str(), NO_LINK); 361 if (((Info.getSize() == strlen(NO_LINK)+1) && 362 (memcmp(Info.getData(), NO_LINK, Info.getSize()) == 0))) return false; 375 DimRpcInfo R((char *) "ServiceHistory", NO_LINK); 376 R.setData((char *) Name.c_str()); 377 if (!EvidenceServer::ServiceOK(&R)) return false; 363 378 364 379 delete[] Buffer; 365 BufferSize = Info.getSize();380 BufferSize = R.getSize(); 366 381 Buffer = new char [BufferSize]; 367 382 368 memcpy(Buffer, Info.getData(), BufferSize);383 memcpy(Buffer, R.getData(), BufferSize); 369 384 Offset = *(int *) Buffer; 370 385 -
Evidence/Evidence.h
r187 r209 47 47 48 48 std::string ServerName; 49 DimService *Status ;49 DimService *Status, *StdOut; 50 50 class ConfigUpdate *ModifyInfo; 51 51 52 52 char InitMsg[STATUS_SIZE]; 53 53 int LastModifyTime; 54 54 char *StdOutText; 55 55 56 static void SignalHandler(int); // static for signal() 56 57 static void Terminate(); // static for set_terminate() … … 65 66 66 67 void State(StateType, const char *, ...); 68 void SetStdOut(char *); 67 69 char* GetConfig(std::string, const char * = NULL); 68 70 static char* ToString(DimInfo *); 69 71 static bool ServiceOK(DimInfo *); 72 static bool ServiceOK(DimRpcInfo *); 70 73 71 74 bool ExitRequest; … … 81 84 int BufferSize; 82 85 int Offset; 83 int Delay;84 int LastUpdate;85 86 86 87 public: 87 EvidenceHistory(std::string , int = 10);88 EvidenceHistory(std::string); 88 89 ~EvidenceHistory(); 89 90 -
Evidence/readme.txt
r187 r209 21 21 22 22 - Oliver Grimm, 18/1/2010 23 24 Version history 25 --------------- 26 27 19/5/2010 Service histories now available via DimRpc from DColl, not via .hist service 28 When regular expression compiling results in error, State is set to ERROR, not 29 FATAL. The erroneous expression is ignored in the following. 30 23 31 24 32
Note:
See TracChangeset
for help on using the changeset viewer.