File:  [Repository] / checkStation / checkStation.py
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Sun May 15 10:04:27 2005 UTC (19 years, 1 month ago) by dwinter
Branches: MAIN
CVS tags: HEAD
new checker

    1: from OFS.SimpleItem import SimpleItem
    2: from Globals import package_home
    3: 
    4: from Products.PageTemplates.PageTemplateFile import PageTemplateFile
    5: import os
    6: import os.path
    7: import time
    8: 
    9: module_list=['11150','11220','11240','11340','12010','13110','13210','13310','13500','20610','21210','21600','22310','23110','24110','24212','24231','24241','24251','24321','24341','24411','24421','24431','24441','24511','24521','30511','31111','31120','31131','31142','31151','31161','31171','31211','31221','31231','31252','31305','31410','32010','33100']
   10: grepString="DragThing"
   11: 
   12: def zptFile(self, path, orphaned=False):
   13:     """returns a page template file from the product"""
   14:     if orphaned:
   15: 	# unusual case
   16: 	pt=PageTemplateFile(os.path.join(package_home(globals()), path))
   17:     else:
   18: 	pt=PageTemplateFile(os.path.join(package_home(globals()), path)).__of__(self)
   19:     return pt
   20: 
   21: 
   22: def getLastLine(lines):
   23:     """letzte"""
   24:     length=len(lines)
   25: 
   26:     if length>0:
   27:         return lines[length-1]
   28:     else:
   29:         return None
   30: 
   31: def checkFormat(line,formats):
   32:     """checke formats"""
   33:     
   34:     splitted=line.split("GET")
   35:     splitted=splitted[1].split()
   36:     
   37:     splitted2=splitted[0].split("/")
   38: 
   39:     try:
   40:      if splitted2[4][0:3] in formats:
   41:         return True
   42:      else:
   43:         return False
   44:     except:
   45:         return False
   46: 
   47: def cmpLine(line):
   48:     """gibt minuten zwischen letztem Eintrag und localtime"""
   49: 
   50:     # Z2.log
   51:     ## tmp=line.split("[")
   52: ##     tmp=tmp[1].split("]")
   53:     
   54: ##     timestr=tmp[0]
   55:     
   56: ##     
   57: 
   58:     #pound log
   59:     tmp=line.split()
   60:     timestr=tmp[2]
   61:     
   62:     
   63:     split=timestr.split(":")
   64:     time1=int(split[0])*60+int(split[1])
   65: 
   66:     localtime=time.asctime(time.localtime())
   67:     
   68:     split=localtime.split(":")
   69:     time2=int(split[0].split()[3])*60+int(split[1])
   70: 
   71: 
   72:     return time2-time1
   73:     
   74: class checkStation(SimpleItem):
   75:     """test ob station lebt"""
   76: 
   77:     meta_type="check Media Stations"
   78: 
   79:     manage_options=SimpleItem.manage_options+(
   80: 		{'label':'Overview Stations','action':'overviewStations'},
   81:                 {'label':'change check','action':'changeCheckStationForm'},
   82:                 {'label':'checkRebootDead','action':'checkRebootDead'},
   83: 		)
   84:     def asctime(self):
   85:         return time.asctime(tm)
   86:         
   87:     def __init__(self, id, path):
   88:         """init"""
   89:         self.path=path #pfad fuer logfile
   90:         self.id=id
   91:         self.modulOverview={}
   92: 
   93:     def checkStation(self,nr,time=5,formats=["VID"]):
   94:         """check station with nr nr"""
   95:         txt=[]
   96: 
   97:         txtFh = os.popen("grep /%s/ %s | tail "%(nr,self.path))
   98:         for line in txtFh.readlines():
   99:             txt.append(line)
  100: 
  101:         
  102:         line=getLastLine(txt)
  103: 
  104:         if not line:
  105:             return True,"",0
  106: 
  107:         cmpT=cmpLine(line)
  108: 
  109:         if not checkFormat(line,formats):
  110:             # teste ob letzte Zeile vom format in formats
  111: 
  112:             return True,line,cmpT
  113: 
  114: 
  115:         if cmpT > time:
  116:             return False,line,cmpT
  117: 
  118:         else:
  119:             return True,line,cmpT
  120: 
  121:     def getModuls(self):
  122:         """getModuls"""
  123:         return module_list
  124:     def overviewStations(self,time=5):
  125:         """overview"""
  126:         self.REQUEST.SESSION['outTime']=time
  127:         
  128:         zp=zptFile(self,"zpt/overview.zpt")
  129:         return zp()
  130: 
  131:     ips={"22222":"127.0.0.1","33335":"127.0.0.2"}
  132:     
  133:     def rebootStation(self,modul):
  134:         """neustart"""
  135:         retStr=""
  136:         ip=self.ips[modul]
  137: 
  138:         str="""ssh root@%s "ps -xa | grep %s" """
  139:         rets=os.popen(str%(ip,grepString)).readlines()
  140: 
  141:         for ret in rets:
  142: 
  143:             if ret.find("grep") <= 0:
  144:                 process=ret.split()[0]
  145:                 str="""ssh root@%s kill -9 %s """
  146:                 #os.popen(str%(ip,process)).readlines()
  147:                 retStr+=str%(ip,process) + "/n"
  148:         return retStr
  149:         
  150:     def checkRebootDead(self,timer=5):
  151:         """checks and reboots non responding firefox"""
  152:         self.lastCheck=time.localtime()
  153:         ret=""
  154:         
  155:         for modul in self.getModuls():
  156: 
  157:             checked=self.checkStation(modul,time=timer,formats=["VID"])
  158:             if not checked[0]:
  159: 
  160:                 ret+=self.rebootStation(modul)
  161:                 if not hasattr(self,'modulOverview'):
  162:                     setattr(self,'modulOverview',{})
  163:                 self.modulOverview[modul]=time.localtime()
  164:                 
  165: 
  166:     def rebootOverview(self):
  167:         """give Overview"""
  168:         pt=zptFile(self,'zpt/rebootOverview.zpt')
  169:         return pt()
  170:     
  171:     def changeCheckStationForm(self):
  172:         """form for change"""
  173: 
  174:         pt=zptFile(self, 'zpt/ChangeCheckStation.zpt')
  175: 
  176:         return pt()
  177: 
  178:     def changeCheckStation(self,path,RESPONSE=None):
  179:         """change"""
  180:         self.path=path
  181: 
  182:         if RESPONSE:
  183:             RESPONSE.redirect("manage_main")
  184: 
  185: 
  186: def manage_addCheckStationForm(self):
  187:     """add"""
  188:     pt=zptFile(self,'zpt/AddCheckStation')
  189:     return pt()
  190: 
  191: def manage_addCheckStation(self,id,path,RESPONSE=None):
  192:     """manage checkstation"""
  193:     
  194:     newObj=checkStation(id,path)
  195:     self._setObject(id,newObj)
  196:     
  197:     if RESPONSE is not None:
  198:         RESPONSE.redirect('manage_main')
  199: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>