;Network Monitor Service Version 5.4 by Chuck Arconi 2/16/2006 AddExtender("WWPST44I.DLL") AddExtender("WWWNT34I.DLL") Addextender("WWIPG34I.DLL") key=RegExistkey(@Regmachine,"SOFTWARE\Arconi") if key != 1 key=RegCreatekey(@REGMACHINE, "SOFTWARE\Arconi") RegSetValue(key, "[Path]", iniloc) RegClosekey(key) else iniloc=RegQueryValue(@REGMACHINE, "SOFTWARE\Arconi[Path]") endif sys_root="%iniloc%\Network_Monitor" tech=strcat(sys_root,"\SrvrMntr1.ini") IntControl(38,1,"%sys_root%\BXerrorlog.txt",0,0) tstfle="%sys_root%\MonitorTest.txt" tst=fileopen(tstfle,"WRITE") fileclose(tst) logtxt="Location->TXT" logmng="Location->Managment" Time=TimeDate() mn_eml="" alt_eml="" alt_eml2="" a=TimeYmdHms() cur_tm=ItemExtract(4, a, ":") cur_tm1=ItemExtract(1, a, ":") cur_tm2=ItemExtract(2, a, ":") cur_tm3=ItemExtract(3, a, ":") cur_tm4=ItemExtract(4, a, ":") mn=0 fx=fileexist(tech) If fx==@FALSE fw=FileOpen(tech,"WRITE") FileClose(fw) IniWritePvt("MNTR", "FirstRun","0", tech) IniWritePvt("MNTR", "FirstRunTime", time, tech) IniWritePvt("MNTR", "LastRunTime", "0",tech) IniWritePvt("MNTR", "DiskSizeLimit", "1000", tech) IniWritePvt("MNTR", "Path", "%sys_root%\Systems\", tech) IniWritePvt("MNTR", "Data", "%sys_root%", tech) IniWritePvt("MNTR", "Down", "%data%\DownTime\DownTime.log", tech) IniWritePvt("MNTR", "ServerLoc", "%sys_root%\Systems", tech) IniWritePvt("MNTR", "DEBUGMODE", "off", tech) IniWritePvt("MNTR", "Domain", "", tech) IniWritePvt("LOGNAME",logmng,"%sys_root%\DownTime\DownTime.log",tech) IniWritePvt("LOGNAME",logtxt,"%sys_root%\ServerLogs\",tech) IniWritePvt("TIMEOUT", "Seconds","600", tech) IniWritePvt("MAILLIST", "EmailAlert00",email, tech) IniWritePvt("MAILLIST", "EmailAlert01",alt_eml, tech) IniWritePvt("MAILLIST", "EmailAlert02",alt_eml, tech) IniWritePvt("HELP", "fileloc","%sys_root%\NetworkMonitorVersion512\index.html", tech) IniWritePvt("SERVICELOOP", "","", tech) IniWritePvt("SUBNETS", "10.x.x","Located in main server room", tech) IniWritePvt("MEAN", "Number=","", tech) IniWritePvt("LOOP", "","", tech) endif data=IniReadPvt("MNTR","Data",sys_root,tech) pth=IniReadPvt("MNTR","Path","%sys_root%\ServerLogs\",tech) downA=IniReadPvt("MNTR","Down","%sys_root%\DownTime\DownTime.log",tech) srvs=IniReadPvt("MNTR","ServerLoc","%sys_root%\ServerList",tech) debugmode=IniReadPvt("MNTR","DEBUGMODE","off",tech) if debugmode=="on" debug(@ON) IntControl(38,0,"%sys_root%\BXerrorlog.txt",0,0) else debug(@OFF) endif If direxist(data)==@false dirmake(data) dirmake("%sys_root%\ServerLogs") dirmake("%sys_root%\DownTime") dirmake("%sys_root%\ServerStats") dirmake("%sys_root%\Systems") endif fl_nma=strcat(pth,cur_tm2,"-",cur_tm3,"-",cur_tm1,".log") IniWritePvt("LOGNAME",logtxt,fl_nma,tech) flxst=IniReadPvt("LOGNAME", logtxt,fl_nma,tech) If fileexist(flxst)==@false fl=FileOpen(fl_nma,"WRITE") fileclose(fl) endif ct=TimeYmdHms() cur_tm4=ItemExtract(4, ct, ":") fl_nm2=IniReadPvt( "LOGNAME",logtxt,fl_nma,tech) ft=FileYmdHms(fl_nm2) fle_tm=ItemExtract(3, ft,":") IniWritePvt( "MNTR", "LastRunTime",time,tech ) :START dom=inireadpvt("MNTR","Domain","",tech) mhost=IniReadPvt("MAILSERVER","MS","",tech) fromaddr=IniReadPvt("MAILSERVER","FromAddress","",tech) userid=IniReadPvt("MAILSERVER","User","",tech) password=IniReadPvt("MAILSERVER","Pass","",tech) port=IniReadPvt("MAILSERVER","UNPort","",tech) lx=0 cur_log=IniReadPvt("LOGNAME",logtxt,fl_nma,tech) email_list00=IniReadPvt("MAILLIST","EmailAlert00","", tech) email_list01=IniReadPvt("MAILLIST","EmailAlert01","0", tech) email_list02=IniReadPvt("MAILLIST","EmailAlert02","", tech) email_virus=IniReadPvt("MAILLIST","EmailAlertAV","0", tech) b=FileYmdHms(cur_log) fle_tm=ItemExtract(3, b,":") if fle_tm != cur_tm3 lx=1 IniDeletePvt("LOOP",@WHOLESECTION,tech ) a=TimeYmdHms() cur_tm=ItemExtract(4, a, ":") b=FileYmdHms(tech) fle_tm=ItemExtract(4, b, ":") cur_tm1=ItemExtract(1, a, ":") cur_tm2=ItemExtract(2, a, ":") cur_tm3=ItemExtract(3, a, ":") cur_tm4=ItemExtract(4, a, ":") fl_nm1=strcat(pth,cur_tm2,"-",cur_tm3,"-",cur_tm1,".log") fl=FileOpen(fl_nm1,"WRITE") FileClose(fl) IniWritePvt("LOGNAME", logtxt,fl_nm1,tech) Gosub ALIVE endif c_flag=0 x=IniReadPvt("TIMEOUT", "Seconds", "0", tech) SvcSetState(4) TimeDelay(x) fl_nm1=IniReadPvt("LOGNAME", logtxt,fl_nma,tech) down=IniReadPvt("LOGNAME",logmng,downA,tech) FW=0 :MONITOR sendmail=1 DirChange(srvs) Time=TimeDate() re_st=IniReadPvt("MEAN", "Number", "0", tech) if re_st != "" mn=re_st +1 else re_st="0" mn="0" endif IniWritePvt("MEAN","Number", mn, tech ) fl_nm1=fileopen(fl_nm1,"APPEND") down=fileopen(downA,"APPEND") ;H=IniReadPvt( "LIST", "Location", "", tech) mon=strcat(srvs,"\*.on") srvscnt=FileItemize(mon) num=itemcount(srvscnt,@tab) service=0 ;*****{SORT THRU LIST OF SERVERS}***** For m = 1 to num x=itemextract(m,srvscnt,@tab) Gosub MNTR next FileClose(fl_nm1) FileClose(down) goto START ;*****{ START CHECKING SYSTEMS }****** :MNTR state=0 c=FileFullName(x) ;srvnm=FileMapName(x,"*") srvnm=IniReadPvt("NAME","Srv", "", c ) Cont=IniReadPvt("CONTACT","Mgr", "", c ) Email=IniReadPvt("CONTACT","Email", "", c ) N_Srvr=inireadpvt("NAME","Sys","",c) host=strcat(srvnm,".",dom) test00=ipPing(host,5) Errormode(@OFF) If test00==@FALSE test00=ipPing(host,5) if test00 == @FALSE err=ipGetLastErr() endif endif If test00==@TRUE sub=ipHost2Addr(host,1) dot="." sub1=ItemExtract (1,sub,dot) sub2=ItemExtract (2,sub,dot) sub3=ItemExtract (3,sub,dot) sub4=strcat(sub1,".",sub2,".",sub3,".") location=IniReadPvt("SUBNETS", sub4, "0", tech) location=strcat("%sub% ",location) if N_Srvr==0 then filewrite(fl_nm1,"%srvnm% is up: %time%") If N_Srvr==1 tstname=strcat("\\",srvnm,"\c$") copy_tst=filecopy(tstfle,tstname,@False) If copy_tst ==@TRUE filewrite(fl_nm1,"%srvnm% is up and accessible: %time%") pass2="/s >" pass3='"%sys_root%\serverstats\%srvnm%.txt"' if lx !=0 then runhidewait('%sys_root%\up.bat','%srvnm% %pass2% %pass3%') drvs=IniItemizePvt("DRV", c ) numdrv=itemcount(drvs,@tab) ;*******{DRIVE CHECKING}*********** for d =1 to numdrv If numdrv !=0 dv=itemextract(d,drvs,@tab) szlmt=IniReadPvt("DRV",dv, "", c ) If dv != "" srvtst=strcat("\\",SrvNm,"\",dv,"$") MB=1024*1024 sz = Diskfree(srvtst) szmb= Int(sz / MB) endif If szmb < szlmt Filewrite(fl_nm1,"*****") Filewrite(fl_nm1,"%srvnm% disk free space low: %srvdsp%=%szmb% megabytes - %time%") Filewrite(fl_nm1,"*****") Filewrite(down,"%srvnm% DiskFreeSpace low: %srvdsp%=%szmb% megabytes - %time%") tolist=email_list00 subject="%srvnm% Disk space low: drive= %srvdsp% -%szmb% Free space" msg="%srvnm% Disk space low: drive= %srvdsp% Total free space= %szmb% megabytes. The minimum free space for this drive is %szlmt% -%time% %@CRLF%Contact Information= %cont% %@CRLF%%location%" gosub loopset if dv == "c" then gosub cleanup endif dsktxt=strcat("Free space on ",dv," = ",szmb," megabytes"," | Threshold: ",szlmt) filewrite(fl_nm1," %dsktxt%") endif next ;*******{SERVICE CHECKING}********* srvcs=IniItemizePvt("SERVICE", c ) srvcnt=itemcount(srvcs,@tab) for s = 1 to srvcnt sc=itemextract(s,srvcs,@tab) service=IniReadPvt("SERVICE",sc,"", c ) If service != 0 sv_sc=strcat(srvnm,"-",sc) state_exist=wntSvcStatus("\\%srvnm%",sc,1000,0) If state_exist==@FALSE txt1=strcat("*{",sc,"}*"," - service is not installed.") Filewrite(fl_nm1," %txt1%") tolist=email_list00 subject="%srvnm% - %sc% service is not installed: %time%" msg="%srvnm% - %sc% service is not installed or has been uninstalled: %time% %@CRLF%Contact Information= %cont% %@CRLF%%location%" gosub loopset else state = wntSvcStatus("\\%srvnm%", sc,1000,2) If state==1 Errormode(@OFF) wntSvcStart("\\%srvnm%",sc,1000,"","") Errormode(@ON) tolist=email_list00 subject="%srvnm% is up but %sc% service is stopped:%time%" msg="%srvnm% is up but %sc% service is stopped: Time:%time%%@CRLF%Attempting to restart service.%@CRLF%%location%" time=TimeDate() txt1=strcat("***{",sc,"}***"," - service is STOPPED.") Filewrite(fl_nm1," %txt1%") Filewrite(down,"%srvnm% CanBePinged: %sc% service-stopped: %time%") gosub serviceloopset timedelay(10) endif state = wntSvcStatus("\\%srvnm%", sc,1000,2) If state==4 servcini=inireadpvt("SERVICELOOP",sv_sc,"",tech) if servcini !="" IniDeletePvt( "SERVICELOOP", sv_sc, tech ) time=TimeDate() Filewrite(fl_nm1,"*****") txt=strcat(srvnm," and ",sc," service is up:",time) Filewrite(fl_nm1,txt) Filewrite(fl_nm1,"Service condition: Stopped, Service was succesfully restarted.") Filewrite(fl_nm1,"*****") Filewrite(down,"%srvnm% %sc% service condition was: Stopped, Service was succesfully restarted.") tolist=email_list00 subject="%srvnm% - %sc% service was restarted" msg="%srvnm% is up and %sc% service was restarted: Time:%time% %@CRLF%Contact Information= %cont% %@CRLF%%location%" gosub SENTALERT endif endif endif endif next else Filewrite(fl_nm1,"*****") Filewrite(fl_nm1,"%srvnm% can be pinged but is not accessible: %time%") Filewrite(fl_nm1,"*****") Filewrite(down,"%srvnm% CanBePinged-IS NOT ACCESSIBLE: %time%") tolist=email_list00 subject="%srvnm% can be pinged but is not accessible: %time%" msg="%srvnm% can be pinged but is not accessible: %time% %@CRLF%Contact Information= %cont% %@CRLF%%location%" goto loopset endif endif endif Errormode(@ON) If test00==@FALSE Filewrite(fl_nm1,"*****") Filewrite(fl_nm1,"%srvnm% cannot be pinged: %time% ErrorCode=%err%") Filewrite(fl_nm1,"*****") Filewrite(down,"%srvnm% CannotBePinged: %time% ErrorCode=%err%") tolist=email_list00 subject="%srvnm% cannot be pinged: %time%" msg="%srvnm% cannot be pinged: %time% ErrorCode=%err% %@CRLF%Contact Information= %cont% %@CRLF%%location%" goto loopset endif return :loopset ;*****{ NOTIFACATION HOLD LOOP }****** z = IniReadPvt( "LOOP", srvnm, "", tech) if z=="" IniWritePvt( "LOOP", srvnm, "0",tech) goto SENTALERT else switch z case 0 r= z + 1 IniWritePvt( "LOOP", srvnm, r, tech) break case 1 r= z + 1 IniWritePvt( "LOOP", srvnm, r, tech) break case 2 r= z + 1 IniWritePvt( "LOOP", srvnm, r, tech) break case 3 r= z + 1 IniWritePvt( "LOOP", srvnm, r, tech) break case 4 r= z + 1 IniWritePvt( "LOOP", srvnm, r, tech) break case 5 IniDeletePvt( "LOOP", srvnm, tech ) break endswitch endif return :serviceloopset ;*****{ NOTIFACATION HOLD SERVICELOOP }****** z = IniReadPvt( "SERVICELOOP", sv_sc, "", tech) if z=="" IniWritePvt( "SERVICELOOP", sv_sc, "0",tech) goto SENTALERT else switch z case 0 r= z + 1 IniWritePvt( "SERVICELOOP", sv_sc, r, tech) break case 1 r= z + 1 IniWritePvt( "SERVICELOOP", sv_sc, r, tech) break case 2 r= z + 1 IniWritePvt( "SERVICELOOP", sv_sc, r, tech) break case 3 r= z + 1 IniWritePvt( "SERVICELOOP", sv_sc, r, tech) break case 4 r= z + 1 IniWritePvt( "SERVICELOOP", sv_sc, r, tech) break case 5 IniDeletePvt( "SERVICELOOP", sv_sc, tech ) tolist=email_list00 subject="%srvnm% %sc% service is still down: %time%" msg="%srvnm% %sc% service is still down after repeated attempts to restart.%@CRLF%Please manualy check service or remove from monitoring.%@CRLF%Contact Information= %cont% %@CRLF%%location%" gosub SENTALERT break endswitch endif return ;*****{ SEND MAIL ALERT }****** :SENTALERT if service==5 then return If service==2 cclist=email_virus else cclist=email_list02 endif bcclist="" attachments="" flags="1" kInit(mhost,fromaddr,userid,password,port) kDest(tolist,cclist,bcclist) kSendText(subject,msg,attachments,flags) return :ALIVE cclist="" bcclist="" attachments="" flags="1" tolist=email_list01 subject="Server monitor is up %time%" msg="Server monitor is up and a new log file has been started. The log file name and location is: %fl_nm1%" kInit(mhost,fromaddr,userid,password,port) kDest(tolist,cclist,bcclist) kSendText(subject,msg,attachments,flags) return :CLEANALERT1 cclist="" bcclist="" attachments="" flags="1" tolist=email_list01 subject="No Cleanup Folder" msg="No Cleanup folder has been defined. Please create a cleanup folder and update the srvmntr.ini file with the location. Further information can be found in the help file." kInit(mhost,fromaddr,userid,password,port) kDest(tolist,cclist,bcclist) kSendText(subject,msg,attachments,flags) return :CLEANALERT2 cclist="" bcclist="" attachments="" flags="1" tolist=email_list01 subject="Cleanup on %srvtst% completed" msg="Cleanup has been completed. A disk space low state was deteced and cleanup was performed. All temp data has been moved to the designated location. Further information can be found in the help file." kInit(mhost,fromaddr,userid,password,port) kDest(tolist,cclist,bcclist) kSendText(subject,msg,attachments,flags) return :CLEANUP if srvs=="" goto CLEANALERT1 return endif clnuploc=IniReadPvt("MNTR","cleanup","",tech) temp="\temp" wtemp="\windows\temp" wstemp= "\windows\system32\temp" sourcea = strcat(srvtst,temp) sourceb = strcat(srvtst,wtemp) sourcec = strcat(srvtst,wstemp) desta =strcat(srvrA,clnuploc,temp) destb =strcat(srvrA,clnuploc,wtemp) destc =strcat(srvrA,clnuploc,wstemp) if direxist(sourcea)==@TRUE ab=strcat(sourcea," ",desta," /MOVE /E") runhide("c:\robocopy.exe",ab) endif if direxist(sourceb)==@TRUE abc=strcat(sourceb," ",destb," /MOVE /E") runhide("c:\robocopy.exe",abc) endif if direxist(sourcec)==@TRUE abcd=strcat(sourcec," ",destc," /MOVE /E") runhide("c:\robocopy.exe",abcd) endif gosub CLEANALERT2 return