Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
2965 lines (2594 loc) · 90.5 KB
/
Copy pathmain.cpp
File metadata and controls
2965 lines (2594 loc) · 90.5 KB
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
10BC8
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************************
* $Id: main.cpp 1976 2013-02-08 16:50:01Z dhill $
*
* Copyright (C) 2009-2012 Calpont Corporation
*
* All rights reserved
******************************************************************************************/
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/version.hpp>
namespace bi=boost::interprocess;
#include "processmonitor.h"
#include "installdir.h"
using namespace std;
using namespace messageqcpp;
using namespace processmonitor;
using namespace oam;
using namespace logging;
using namespace snmpmanager;
using namespace config;
//using namespace procheartbeat;
static void messageThread(MonitorConfig config);
static void statusControlThread();
static void sigchldHandleThread();
static void SIGCHLDHandler(int signal_number);
static void chldHandleThread(MonitorConfig config);
static void sigHupHandler(int sig);
static void mysqlMonitorThread(MonitorConfig config);
string systemOAM;
string dm_server;
string cloud;
string GlusterConfig = "n";
void updateShareMemory(processStatusList* aPtr);
bool runStandby = false;
bool processInitComplete = false;
bool rootUser = true;
string USER = "root";
//extern std::string gOAMParentModuleName;
extern bool gOAMParentModuleFlag;
pthread_mutex_t STATUS_LOCK;
/******************************************************************************************
* @brief main
*
* purpose: Launch boot child processes and sit on read for incoming messages
*
******************************************************************************************/
int main(int argc, char **argv)
{
#ifndef _MSC_VER
setuid(0); // set effective ID to root; ignore return status
#endif
if (argc > 1 && string(argv[1]) == "--daemon")
{
if (fork() != 0) return 0;
umask(0);
setsid();
chdir("/");
close(0);
close(1);
close(2);
open("/dev/null", O_RDONLY);
open("/dev/null", O_WRONLY);
open("/dev/null", O_WRONLY);
}
Oam oam;
MonitorLog log;
MonitorConfig config;
ProcessMonitor aMonitor(config, log);
log.writeLog(__LINE__, " ");
log.writeLog(__LINE__, "**********Process Monitor Started**********");
log.writeLog(__LINE__, " ", LOG_TYPE_DEBUG);
log.writeLog(__LINE__, "**********Process Monitor Started**********", LOG_TYPE_DEBUG);
//Ignore SIGPIPE signals
signal(SIGPIPE, SIG_IGN);
//create SIGHUP handler to get configuration updates
signal(SIGHUP, sigHupHandler);
//check if root-user
int user;
user = getuid();
if (user != 0)
rootUser = false;
char* p= getenv("USER");
if (p && *p)
USER = p;
// get and set locale language
string systemLang = "C";
try{
oam.getSystemConfig("SystemLang", systemLang);
}
catch(...)
{
systemLang = "C";
}
setlocale(LC_ALL, systemLang.c_str());
// if amazon cloud, check and update Instance IP Addresses and volumes
try {
oam.getSystemConfig( "Cloud", cloud);
}
catch(...) {}
if ( cloud == "amazon" ) {
if(!aMonitor.amazonIPCheck()) {
string cmd = startup::StartUp::installDir() + "/bin/infinidb stop > /dev/null 2>&1";
system(cmd.c_str());
exit(1);
}
}
//get gluster config
try {
oam.getSystemConfig( "GlusterConfig", GlusterConfig);
}
catch(...)
{
GlusterConfig = "n";
}
if ( GlusterConfig == "y" ) {
system("mount -a > /dev/null 2>&1");
}
//define entry if missing
Config* sysConfig = Config::makeConfig();
if ( gOAMParentModuleFlag )
{
string PrimaryUMModuleName;
try {
oam.getSystemConfig("PrimaryUMModuleName", PrimaryUMModuleName);
}
catch(...) {
sysConfig->setConfig("SystemConfig", "PrimaryUMModuleName", oam::UnassignedName);
sysConfig->write();
}
}
// if single server amazon ami, calculate TotalUmMemory
/* string ami;
try {
oam.getSystemConfig( "AMI", ami);
}
catch(...) {}
if ( ami == "y" )
aMonitor.calTotalUmMemory();
*/
if ( config.moduleType() == "pm" )
{
if ( gOAMParentModuleFlag )
log.writeLog(__LINE__, "ProcMon: Starting as ACTIVE Parent", LOG_TYPE_DEBUG);
else
log.writeLog(__LINE__, "ProcMon: Starting as NON-ACTIVE Parent", LOG_TYPE_DEBUG);
}
// create message thread
pthread_t MessageThread;
int ret = pthread_create (&MessageThread, NULL, (void*(*)(void*)) &messageThread, &config);
if ( ret != 0 )
log.writeLog(__LINE__, "pthread_create failed, return code = " + oam.itoa(ret), LOG_TYPE_ERROR);
//create and mount data directories
aMonitor.createDataDirs(cloud);
//check if this module is recovering after a reboot for an active OAM parent state
ByteStream msg;
ByteStream::byte requestID = GETPARENTOAMMODULE;
msg << requestID;
//check if currently configured as Parent OAM Module on startup
if ( gOAMParentModuleFlag ) {
if ( config.OAMStandbyName() != oam::UnassignedName ) {
//try for 20 minutes checking if the standby node is up
string parentOAMModule;
log.writeLog(__LINE__, "starting has parent, double check. checking with old Standby Module", LOG_TYPE_DEBUG);
int count = 0;
for (; count < 120 ; count++)
{
parentOAMModule = aMonitor.sendMsgProcMon1( config.OAMStandbyName(), msg, requestID );
if ( parentOAMModule != "FAILED" )
break;
log.writeLog(__LINE__, "Standby PM not responding, retrying", LOG_TYPE_WARNING);
sleep(10);
}
// check if standby never replied, if so, shutdown
if ( count >= 120 ) {
log.writeLog(__LINE__, "Standby PM not responding, infinidb shutting down", LOG_TYPE_CRITICAL);
//Set the alarm
aMonitor.sendAlarm(config.moduleName().c_str(), STARTUP_DIAGNOTICS_FAILURE, SET);
sleep (1);
string cmd = startup::StartUp::installDir() + "/bin/infinidb stop > /dev/null 2>&1";
system(cmd.c_str());
}
log.writeLog(__LINE__, "Old Standby has moduleparentOAMModule = " + parentOAMModule, LOG_TYPE_DEBUG);
if ( parentOAMModule != config.moduleName() ) {
gOAMParentModuleFlag = false;
log.writeLog(__LINE__, "NOT Parent OAM Module", LOG_TYPE_DEBUG);
try
{
Config* sysConfig = Config::makeConfig();
// get Standby IP address
ModuleConfig moduleconfig;
oam.getSystemConfig(config.OAMStandbyName(), moduleconfig);
HostConfigList::iterator pt1 = moduleconfig.hostConfigList.begin();
string IPaddr = (*pt1).IPAddr;
sysConfig->setConfig("ProcMgr", "IPAddr", IPaddr);
log.writeLog(__LINE__, "set ProcMgr IPaddr to Old Standby Module: " + IPaddr, LOG_TYPE_DEBUG);
//update Calpont Config table
try {
sysConfig->write();
sleep(1);
}
catch(...)
{
log.writeLog(__LINE__, "ERROR: sysConfig->write", LOG_TYPE_ERROR);
}
}
catch(...)
{
log.writeLog(__LINE__, "ERROR: makeConfig failed", LOG_TYPE_ERROR);
}
}
}
}
// not active Parent, get updated Calpont.xml, retry in case ProcMgr isn't up yet
if ( !gOAMParentModuleFlag )
{
int count = 0;
while(true)
{
try {
oam.distributeConfigFile(config.moduleName());
log.writeLog(__LINE__, "Successfull return from distributeConfigFile", LOG_TYPE_DEBUG);
break;
}
catch(...) {
count++;
if (count > 60 ) {
count = 0;
log.writeLog(__LINE__, "error return from distributeConfigFile, waiting for Active ProcMgr to start", LOG_TYPE_DEBUG);
}
sleep(1);
}
}
//re-read local system info with new Calpont.xml
sleep(1);
MonitorConfig config;
}
int moduleStatus;
if ( config.OAMParentName() == oam::UnassignedName ) {
cerr << endl << "OAMParentModuleName == oam::UnassignedName, exiting " << endl;
log.writeLog(__LINE__, "OAMParentModuleName == oam::UnassignedName, exiting", LOG_TYPE_CRITICAL);
exit (-1);
}
// not OAM parent module, delay starting until a successful get status is performed
// makes sure the Parent OAM ProcMon is fully ready
if ( !gOAMParentModuleFlag) {
while(true)
{
try {
bool degraded;
oam.getModuleStatus(config.moduleName(), moduleStatus, degraded);
break;
}
catch(...)
{
log.writeLog(__LINE__, "error return from getModuleStatus", LOG_TYPE_DEBUG);
sleep (1);
}
}
}
//check if module is in a DISABLED state
bool DISABLED = false;
if ( moduleStatus == oam::MAN_DISABLED ||
moduleStatus == oam::AUTO_DISABLED )
DISABLED = true;
if ( config.moduleType() == "pm" ) {
int retry = 0;
for ( ; retry < 20 ; retry++ )
{
int ret = aMonitor.checkDataMount();
if ( ret == oam::API_SUCCESS)
break;
if (ret == API_INVALID_PARAMETER) {
//no dbroots assigned, treat as disabled
if ( !DISABLED )
DISABLED=true;
}
if ( DISABLED ) {
log.writeLog(__LINE__, "ERROR: checkDataMount to failed, module is disabled, continuing", LOG_TYPE_WARNING);
break;
}
else
log.writeLog(__LINE__, "ERROR: checkDataMount to failed, retrying", LOG_TYPE_WARNING);
//send notification about the mount setup failure
oam.sendDeviceNotification(config.moduleName(), DBROOT_MOUNT_FAILURE);
sleep(30);
}
if ( retry == 20 )
{
log.writeLog(__LINE__, "Check DB mounts failed, infinidb shutting down", LOG_TYPE_CRITICAL);
//Set the alarm
aMonitor.sendAlarm(config.moduleName().c_str(), STARTUP_DIAGNOTICS_FAILURE, SET);
sleep (1);
string cmd = startup::StartUp::installDir() + "/bin/infinidb stop > /dev/null 2>&1";
system(cmd.c_str());
}
if ( !gOAMParentModuleFlag ) {
runStandby = true;
// delete any old active alarm log file
unlink ("/var/log/Calpont/activeAlarms");
}
//launch Status table control thread on 'pm' modules
pthread_t statusThread;
int ret = pthread_create (&statusThread, NULL, (void*(*)(void*)) &statusControlThread, NULL);
if ( ret != 0 )
log.writeLog(__LINE__, "pthread_create failed, return code = " + oam.itoa(ret), LOG_TYPE_ERROR);
sleep(5); // give the Status thread time to fully initialize
/* if ( config.ServerInstallType() == oam::INSTALL_COMBINE_DM_UM_PM ) {
// start mysqld to make sure it's running
// need for addmodule command
try {
oam.actionMysqlCalpont(MYSQL_START);
}
catch(...)
{}
}
*/ }
/* else
{ // um, start mysqld to make sure it's running
// need for reconfiguremodule command
try {
oam.actionMysqlCalpont(MYSQL_START);
}
catch(...)
{}
}
*/
// stop mysqld to make sure it's not running after reboot
try {
oam.actionMysqlCalpont(MYSQL_STOP);
}
catch(...)
{}
SystemStatus systemstatus;
try
{
oam.getSystemStatus(systemstatus);
}
catch(...)
{}
// determine Standby OAM Module, if needed
if ( gOAMParentModuleFlag &&
config.OAMStandbyName() == oam::UnassignedName &&
config.ServerInstallType() != oam::INSTALL_COMBINE_DM_UM_PM ) {
for( unsigned int i = 0 ; i < systemstatus.systemmodulestatus.modulestatus.size(); i++)
{
string moduleName = systemstatus.systemmodulestatus.modulestatus[i].Module;
if ( moduleName.substr(0,MAX_MODULE_TYPE_SIZE) == "pm" &&
moduleName != config.moduleName() ) {
// multi pm system
int moduleStatus = systemstatus.systemmodulestatus.modulestatus[i].ModuleOpState;
if ( moduleStatus != oam::MAN_DISABLED &&
moduleStatus != oam::AUTO_DISABLED ) {
// found a standby candidate
oam.setSystemConfig("StandbyOAMModuleName", moduleName);
// update Standby IP Address
ModuleConfig moduleconfig;
oam.getSystemConfig(moduleName, moduleconfig);
HostConfigList::iterator pt1 = moduleconfig.hostConfigList.begin();
string standbyIPaddr = (*pt1).IPAddr;
Config* sysConfig2 = Config::makeConfig();
sysConfig2->setConfig("ProcStatusControlStandby", "IPAddr", standbyIPaddr);
sysConfig2->write();
oam.setHotStandbyPM(standbyIPaddr);
log.writeLog(__LINE__, "Calpont.xml Standby OAM updated : " + moduleName + ":" + standbyIPaddr, LOG_TYPE_DEBUG);
log.writeLog(__LINE__, "Set Standby Module = " + moduleName, LOG_TYPE_DEBUG);
try {
oam.distributeConfigFile(config.moduleName());
log.writeLog(__LINE__, "successfull return from distributeConfigFile", LOG_TYPE_DEBUG);
}
catch(...)
{}
break;
}
}
}
}
// non Parent Module, don't start until process-manager is up on parent module
// away to control starting mutliple Active Process-Managers
if ( !gOAMParentModuleFlag && config.moduleType() == "pm" ) {
string parentOAMModuleName;
while(true)
{
try{
Config* sysConfig = Config::makeConfig();
parentOAMModuleName = sysConfig->getConfig("SystemConfig", "ParentOAMModuleName");
if ( parentOAMModuleName != oam::UnassignedName )
break;
sleep(1);
log.writeLog(__LINE__, "Waiting for process-manager on parent module", LOG_TYPE_ERROR);
}
catch(...)
{
log.writeLog(__LINE__, "Problem getting the ParentOAMModuleName key from the Calpont System Configuration file", LOG_TYPE_ERROR);
exit(-1);
}
}
MonitorConfig config;
while(true)
{
try {
Oam oam;
ProcessStatus procstat;
oam.getProcessStatus("ProcessManager", parentOAMModuleName, procstat);
if ( procstat.ProcessOpState == oam::ACTIVE )
break;
sleep(1);
log.writeLog(__LINE__, "Waiting for process-manager to go ACTIVE", LOG_TYPE_ERROR);
}
catch (exception& ex)
{
string error = ex.what();
log.writeLog(__LINE__, "EXCEPTION ERROR on getProcessStatus: " + error, LOG_TYPE_ERROR);
}
catch(...)
{
log.writeLog(__LINE__, "EXCEPTION ERROR on getProcessStatus: Caught unknown exception!", LOG_TYPE_ERROR);
}
}
}
//Mark this process AUTO-OFFLINE
aMonitor.updateProcessInfo("ProcessMonitor", oam::AUTO_OFFLINE, getpid());
//handle SIGCHLD signal
pthread_t signalThread;
ret = pthread_create (&signalThread, NULL, (void*(*)(void*)) &sigchldHandleThread, NULL);
if ( ret != 0 )
log.writeLog(__LINE__, "pthread_create failed, return code = " + oam.itoa(ret), LOG_TYPE_ERROR);
//mysql status monitor thread
pthread_t mysqlThread;
ret = pthread_create (&mysqlThread, NULL, (void*(*)(void*)) &mysqlMonitorThread, NULL);
if ( ret != 0 )
log.writeLog(__LINE__, "pthread_create failed, return code = " + oam.itoa(ret), LOG_TYPE_ERROR);
//update syslog file priviledges
aMonitor.changeModLog();
//update crontab
// aMonitor.changeCrontab();
//Read ProcessConfig file to get process list belong to this process monitor
SystemProcessConfig systemprocessconfig;
try {
oam.getProcessConfig(systemprocessconfig);
}
catch (exception& ex)
{
string error = ex.what();
log.writeLog(__LINE__, "EXCEPTION ERROR on getProcessConfig: " + error, LOG_TYPE_ERROR);
}
catch(...)
{
log.writeLog(__LINE__, "EXCEPTION ERROR on getProcessConfig: Caught unknown exception!", LOG_TYPE_ERROR);
}
string OAMParentModuleType = config.OAMParentName().substr(0,2);
//Build a map for application name tag and launch ID for this Process-Monitor
for( unsigned int i = 0 ; i < systemprocessconfig.processconfig.size(); i++)
{
//skip if both BootLaunch and LaunchID are 0
if ( systemprocessconfig.processconfig[i].BootLaunch == 0 &&
systemprocessconfig.processconfig[i].LaunchID == 0 )
continue;
if (systemprocessconfig.processconfig[i].ModuleType == config.moduleType() ||
systemprocessconfig.processconfig[i].ModuleType == "ChildExtOAMModule" ||
(systemprocessconfig.processconfig[i].ModuleType == "ChildOAMModule" ) ||
(systemprocessconfig.processconfig[i].ModuleType == "ParentOAMModule" &&
config.moduleType() == OAMParentModuleType ) )
{
// If Process Monitor, update local state
if ( systemprocessconfig.processconfig[i].ProcessName == "ProcessMonitor")
{
config.buildList(systemprocessconfig.processconfig[i].ModuleType,
systemprocessconfig.processconfig[i].ProcessName,
systemprocessconfig.processconfig[i].ProcessLocation,
systemprocessconfig.processconfig[i].ProcessArgs,
systemprocessconfig.processconfig[i].LaunchID,
getpid(),
oam::AUTO_OFFLINE,
systemprocessconfig.processconfig[i].BootLaunch,
systemprocessconfig.processconfig[i].RunType,
systemprocessconfig.processconfig[i].DepProcessName,
systemprocessconfig.processconfig[i].DepModuleName,
systemprocessconfig.processconfig[i].LogFile);
}
else
{
// Get Last Known Process Status and PID
int state = oam::AUTO_OFFLINE;
int PID = 0;
try {
Oam oam;
ProcessStatus procstat;
oam.getProcessStatus(systemprocessconfig.processconfig[i].ProcessName, config.moduleName(), procstat);
state = procstat.ProcessOpState;
PID = procstat.ProcessID;
}
catch (exception& ex)
{
string error = ex.what();
log.writeLog(__LINE__, "EXCEPTION ERROR on getProcessStatus: " + error, LOG_TYPE_ERROR);
}
catch(...)
{
log.writeLog(__LINE__, "EXCEPTION ERROR on getProcessStatus: Caught unknown exception!", LOG_TYPE_ERROR);
}
config.buildList(systemprocessconfig.processconfig[i].ModuleType,
systemprocessconfig.processconfig[i].ProcessName,
systemprocessconfig.processconfig[i].ProcessLocation,
systemprocessconfig.processconfig[i].ProcessArgs,
systemprocessconfig.processconfig[i].LaunchID,
PID,
state,
systemprocessconfig.processconfig[i].BootLaunch,
systemprocessconfig.processconfig[i].RunType,
systemprocessconfig.processconfig[i].DepProcessName,
systemprocessconfig.processconfig[i].DepModuleName,
systemprocessconfig.processconfig[i].LogFile);
}
}
}
if ( systemstatus.SystemOpState != MAN_OFFLINE && !DISABLED) {
// Loop through the process list to check the process current state
// Launch the Processes controlled by the Process-Monitor
processList::iterator listPtr;
processList* aPtr = config.monitoredListPtr();
listPtr = aPtr->begin();
for (; listPtr != aPtr->end(); ++listPtr)
{
// If Process Monitor, skip
if ( (*listPtr).ProcessName == "ProcessMonitor")
continue;
if ((*listPtr).processID != 0)
{
if ((*listPtr).BootLaunch == BOOT_LAUNCH)
{
//Check for SIMPLEX runtype processes
int initType = aMonitor.checkSpecialProcessState( (*listPtr).ProcessName, (*listPtr).RunType, (*listPtr).ProcessModuleType );
if ( initType == oam::COLD_STANDBY ) {
//there is a mate active, skip
(*listPtr).state = oam::COLD_STANDBY;
// sleep(1);
continue;
}
else
if ( initType == oam::MAN_INIT )
initType= oam::AUTO_INIT;
//Check the process current state
if ((kill((*listPtr).processID, 0)) != 0
&& (*listPtr).state != oam::MAN_OFFLINE)
{
//The process died, start the process, reset the pid and time
//Set the alarm
aMonitor.sendAlarm((*listPtr).ProcessName.c_str(), PROCESS_DOWN_AUTO, SET);
//stop the process first to make sure it's gone
aMonitor.stopProcess((*listPtr).processID,
(*listPtr).ProcessName,
(*listPtr).ProcessLocation,
oam::FORCEFUL,
false);
//Start the process
(*listPtr).processID = aMonitor.startProcess( (*listPtr).ProcessModuleType,
(*listPtr).ProcessName,
(*listPtr).ProcessLocation,
(*listPtr).ProcessArgs,
(*listPtr).launchID,
(*listPtr).BootLaunch,
(*listPtr).RunType,
(*listPtr).DepProcessName,
(*listPtr).DepModuleName,
(*listPtr).LogFile,
initType);
string restartStatus;
if ( (*listPtr).processID == oam::API_MINOR_FAILURE ||
(*listPtr).processID == oam::API_FAILURE )
// restart failed
string restartStatus = " restart failed!!";
else
string restartStatus = " restarted successfully!!";
log.writeLog(__LINE__, restartStatus, LOG_TYPE_INFO);
}
}
}
else if ((*listPtr).BootLaunch == BOOT_LAUNCH)
{
//Check for SIMPLEX runtype processes
int initType = aMonitor.checkSpecialProcessState( (*listPtr).ProcessName, (*listPtr).RunType, (*listPtr).ProcessModuleType );
if ( initType == oam::COLD_STANDBY ) {
//there is a mate active, skip
(*listPtr).state = oam::COLD_STANDBY;
sleep(1);
continue;
}
else
if ( initType == oam::MAN_INIT )
initType= oam::AUTO_INIT;
if ((*listPtr).state == oam::MAN_OFFLINE)
continue;
//stop the process first to make sure it's gone
aMonitor.stopProcess((*listPtr).processID,
(*listPtr).ProcessName,
(*listPtr).ProcessLocation,
oam::FORCEFUL,
false);
//Start the boot time processes, set its state, ProcessID
(*listPtr).processID = aMonitor.startProcess((*listPtr).ProcessModuleType,
(*listPtr).ProcessName,
(*listPtr).ProcessLocation,
(*listPtr).ProcessArgs,
(*listPtr).launchID,
(*listPtr).BootLaunch,
(*listPtr).RunType,
(*listPtr).DepProcessName,
(*listPtr).DepModuleName,
(*listPtr).LogFile,
initType);
string restartStatus;
if ( (*listPtr).processID == oam::API_MINOR_FAILURE ||
(*listPtr).processID == oam::API_FAILURE )
// restart failed
string restartStatus = " restart failed!!";
else
string restartStatus = " restarted successfully!!";
log.writeLog(__LINE__, restartStatus, LOG_TYPE_INFO);
}
} //end of for loop
}
// create process health (monitor) thread
pthread_t processHealthThread;
ret = pthread_create (&processHealthThread, NULL, (void*(*)(void*)) &chldHandleThread, &config);
if ( ret != 0 )
log.writeLog(__LINE__, "pthread_create failed, return code = " + oam.itoa(ret), LOG_TYPE_ERROR);
//Mark this process Init Complete
while(true)
{
try {
oam.processInitComplete("ProcessMonitor");
log.writeLog(__LINE__, "processInitComplete Successfully Called", LOG_TYPE_DEBUG);
}
catch (exception& ex)
{
string error = ex.what();
log.writeLog(__LINE__, "EXCEPTION ERROR on processInitComplete: " + error, LOG_TYPE_ERROR);
// this would fail if Parent OAM Node is down
sleep(1);
continue;
}
catch(...)
{
log.writeLog(__LINE__, "EXCEPTION ERROR on processInitComplete: Caught unknown exception!", LOG_TYPE_ERROR);
// this would fail if Parent OAM Node is down
sleep(1);
continue;
}
for( unsigned int i = 0 ; i < systemprocessconfig.processconfig.size(); i++)
{
// If Process Monitor, update local state
if ( systemprocessconfig.processconfig[i].ProcessName == "ProcessMonitor")
{
config.buildList(systemprocessconfig.processconfig[i].ModuleType,
systemprocessconfig.processconfig[i].ProcessName,
systemprocessconfig.processconfig[i].ProcessLocation,
systemprocessconfig.processconfig[i].ProcessArgs,
systemprocessconfig.processconfig[i].LaunchID,
getpid(),
oam::ACTIVE,
systemprocessconfig.processconfig[i].BootLaunch,
systemprocessconfig.processconfig[i].RunType,
systemprocessconfig.processconfig[i].DepProcessName,
systemprocessconfig.processconfig[i].DepModuleName,
systemprocessconfig.processconfig[i].LogFile);
break;
}
}
break;
//Clear the alarms
aMonitor.sendAlarm("ProcessMonitor", PROCESS_DOWN_MANUAL, CLEAR);
aMonitor.sendAlarm("ProcessMonitor", PROCESS_DOWN_AUTO, CLEAR);
}
//set process init complete and ready to process message request
processInitComplete = true;
// suspend forever
while(true)
{
sleep(1000);
}
}
/******************************************************************************************
* @brief messageThread
*
* purpose: Read incoming messages
*
******************************************************************************************/
static void messageThread(MonitorConfig config)
{
//ProcMon log file
MonitorLog log;
ProcessMonitor aMonitor(config, log);
log.writeLog(__LINE__, "Message Thread started ..", LOG_TYPE_DEBUG);
Oam oam;
string msgPort = config.moduleName() + "_ProcessMonitor";
//ProcMon will wait for request
IOSocket fIos;
//read and cleanup port before trying to use
try {
Config* sysConfig = Config::makeConfig();
string port = sysConfig->getConfig(msgPort, "Port");
string cmd = "fuser -k " + port + "/tcp >/dev/null 2>&1";
if ( !rootUser)
cmd = "sudo fuser -k " + port + "/tcp >/dev/null 2>&1";
system(cmd.c_str());
}
catch(...)
{
}
for (;;)
{
try
{
ByteStream msg;
MessageQueueServer mqs(msgPort);
for (;;)
{
try
{
fIos = mqs.accept();
try
{
msg = fIos.read();
if (msg.length() > 0) {
aMonitor.processMessage(msg, fIos);
}
}
catch (exception& ex)
{
string error = ex.what();
log.writeLog(__LINE__, "EXCEPTION ERROR on fIos.read() for " + msgPort + ", error: " + error, LOG_TYPE_ERROR);
}
catch(...)
{
log.writeLog(__LINE__, "EXCEPTION ERROR on fIos.read() for " + msgPort + ", Caught unknown exception!", LOG_TYPE_ERROR);
}
}
catch (exception& ex)
{
string error = ex.what();
log.writeLog(__LINE__, "EXCEPTION ERROR on mqs.accept() for " + msgPort + ", error: " + error, LOG_TYPE_ERROR);
}
catch(...)
{
log.writeLog(__LINE__, "EXCEPTION ERROR on mqs.accept() for " + msgPort + ", Caught unknown exception!", LOG_TYPE_ERROR);
}
// give time to allow Mgr to read any acks before closing
sleep(1);
fIos.close();
}
}
catch (exception& ex)
{
string error = ex.what();
log.writeLog(__LINE__, "EXCEPTION ERROR on MessageQueueServer for " + msgPort + ": " + error, LOG_TYPE_ERROR);
// takes 2 - 4 minites to free sockets, sleep and retry
sleep(1);
}
catch(...)
{
log.writeLog(__LINE__, "EXCEPTION ERROR on MessageQueueServer for " + msgPort + ": Caught unknown exception!", LOG_TYPE_ERROR);
// takes 2 - 4 minites to free sockets, sleep and retry
sleep(1);
}
}
return;
}
/******************************************************************************************
* @brief mysqlMonitorThread
*
* purpose: monitor mysqld by getting status
*
******************************************************************************************/
static void mysqlMonitorThread(MonitorConfig config)
{
MonitorLog log;
ProcessMonitor aMonitor(config, log);
log.writeLog(__LINE__, "mysqld Monitoring Thread started ..", LOG_TYPE_DEBUG);
Oam oam;
while(true)
{
//read status, whichs set process status
try {
oam.actionMysqlCalpont(MYSQL_STATUS);
}
catch(...)
{}
sleep(10);
}
}
/******************************************************************************************
* @brief sigchldHandleThread / SIGCHLDHandler
*
* purpose: Catch and process dieing child processes
*
******************************************************************************************/
static void sigchldHandleThread()
{
struct sigaction sigchld_action;
memset (&sigchld_action, 0, sizeof (sigchld_action));
sigchld_action.sa_handler = &SIGCHLDHandler;
sigaction(SIGCHLD, &sigchld_action, NULL);
return;
}
static void SIGCHLDHandler(int signal_number)
{
int status;
pid_t procID;
procID = waitpid(-1, &status, WNOHANG);
// procID = wait(&status);
// if (WIFEXITED (status))
return;
}
/******************************************************************************************
* @brief chldHandleThread
*
* purpose: Monitor and process dieing Non SIGCHILD SNMP child processes
* Also validate the internal Process status with the Process-Status disk file
*
******************************************************************************************/
static void chldHandleThread(MonitorConfig config)
{
//ProcMon log file
MonitorLog log;
ProcessMonitor aMonitor(config, log);
log.writeLog(__LINE__, "Child Process Monitoring Thread started ..", LOG_TYPE_DEBUG);
Oam oam;
//Loop through the process list to check the process current state
processList::iterator listPtr;
processList* aPtr = config.monitoredListPtr();
//get dbhealth flag
string DBFunctionalMonitorFlag;
try {
oam.getSystemConfig( "DBFunctionalMonitorFlag", DBFunctionalMonitorFlag);
}
catch(...) {}
int delayCount=0;
while (true)
{
//get process restart configured settings
int processRestartCount = 10;
int processRestartPeriod = 120;
try{
oam.getSystemConfig("ProcessRestartCount", processRestartCount);
oam.getSystemConfig("ProcessRestartPeriod", processRestartPeriod);
}
catch(...)
{
processRestartCount = 10;
processRestartPeriod = 120;
}
listPtr = aPtr->begin();
for (; listPtr != aPtr->end(); ++listPtr)
{
// compare internal process state and PID with system process status
// Issue alarm if system state is INIT for longer than 1 minute
// Update internal process state when in INIT and System is ACTIVE/FAILED
// Updated System process state when AOS and different from internal
int outOfSyncCount = 0;
if ( delayCount == 10 ) {
while(true)
{
int state = (*listPtr).state; //set as default
int PID = (*listPtr).processID; //set as default
try {
ProcessStatus procstat;
oam.getProcessStatus((*listPtr).ProcessName, config.moduleName(), procstat);
state = procstat.ProcessOpState;
PID = procstat.ProcessID;
if (state == oam::BUSY_INIT ) {
// updated local state ot BUSY_INIT
(*listPtr).state = state;
break;
}
if ( (state == oam::AUTO_INIT && (*listPtr).state == oam::AUTO_INIT) ||
(state == oam::MAN_INIT && (*listPtr).state == oam::MAN_INIT) ) {
// get current time in seconds
time_t cal;