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

    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:                 {'label':'Reboot Overview','action':'rebootOverview'},
   84: 		)
   85:     def asctime(self):
   86:         return time.asctime(tm)
   87:         
   88:     def __init__(self, id, path):
   89:         """init"""
   90:         self.path=path #pfad fuer logfile
   91:         self.id=id
   92:         self.modulOverview={}
   93: 
   94:     def checkStation(self,nr,time=5,formats=["VID"]):
   95:         """check station with nr nr"""
   96:         txt=[]
   97: 
   98:         txtFh = os.popen("grep /%s/ %s | tail "%(nr,self.path))
   99:         for line in txtFh.readlines():
  100:             txt.append(line)
  101: 
  102:         
  103:         line=getLastLine(txt)
  104: 
  105:         if not line:
  106:             return True,"",0
  107: 
  108:         cmpT=cmpLine(line)
  109: 
  110:         if not checkFormat(line,formats):
  111:             # teste ob letzte Zeile vom format in formats
  112: 
  113:             return True,line,cmpT
  114: 
  115: 
  116:         if cmpT > time:
  117:             return False,line,cmpT
  118: 
  119:         else:
  120:             return True,line,cmpT
  121: 
  122:     def getModuls(self):
  123:         """getModuls"""
  124:         return module_list
  125:     def overviewStations(self,time=5):
  126:         """overview"""
  127:         self.REQUEST.SESSION['outTime']=time
  128:         
  129:         zp=zptFile(self,"zpt/overview.zpt")
  130:         return zp()
  131: 
  132:     ips={"22222":"127.0.0.1","33335":"127.0.0.2"}
  133:     
  134:     def rebootStation(self,modul):
  135:         """neustart"""
  136:         retStr=""
  137:         ip=self.ips[modul]
  138: 
  139:         str="""ssh root@%s "ps -xa | grep %s" """
  140:         rets=os.popen(str%(ip,grepString)).readlines()
  141: 
  142:         for ret in rets:
  143: 
  144:             if ret.find("grep") <= 0:
  145:                 process=ret.split()[0]
  146:                 str="""ssh root@%s kill -9 %s """
  147:                 #os.popen(str%(ip,process)).readlines()
  148:                 retStr+=str%(ip,process) + "/n"
  149:         return retStr
  150:         
  151:     def checkRebootDead(self,timer=5):
  152:         """checks and reboots non responding firefox"""
  153:         self.lastCheck=time.localtime()
  154:         ret="<html><body><p>"
  155:         
  156:         for modul in self.getModuls():
  157: 
  158:             checked=self.checkStation(modul,time=timer,formats=["VID"])
  159:             if not checked[0]:
  160: 
  161:                 ret+=self.rebootStation(modul)
  162:                 if not hasattr(self,'modulOverview'):
  163:                     setattr(self,'modulOverview',{})
  164:                 self.modulOverview[modul]=time.localtime()
  165: 	
  166: 
  167: 
  168:     def rebootOverview(self):
  169:         """give Overview"""
  170:         pt=zptFile(self,'zpt/rebootOverview.zpt')
  171:         return pt()
  172:     
  173:     def changeCheckStationForm(self):
  174:         """form for change"""
  175: 
  176:         pt=zptFile(self, 'zpt/ChangeCheckStation.zpt')
  177: 
  178:         return pt()
  179: 
  180:     def changeCheckStation(self,path,RESPONSE=None):
  181:         """change"""
  182:         self.path=path
  183: 
  184:         if RESPONSE:
  185:             RESPONSE.redirect("manage_main")
  186: 
  187: 
  188: def manage_addCheckStationForm(self):
  189:     """add"""
  190:     pt=zptFile(self,'zpt/AddCheckStation')
  191:     return pt()
  192: 
  193: def manage_addCheckStation(self,id,path,RESPONSE=None):
  194:     """manage checkstation"""
  195:     
  196:     newObj=checkStation(id,path)
  197:     self._setObject(id,newObj)
  198:     
  199:     if RESPONSE is not None:
  200:         RESPONSE.redirect('manage_main')
  201: 

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