Annotation of checkStation/checkStation.py, revision 1.1.1.1

1.1       dwinter     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=['33335','22222','ff']
                     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:     splitted=line.split("GET")
                     34:     splitted=splitted[1].split()
                     35:     splitted2=splitted[0].split("/")
                     36: 
                     37:     if splitted2[4][0:3] in formats:
                     38:         return True
                     39:     else:
                     40:         return False
                     41: 
                     42: 
                     43: def cmpLine(line):
                     44:     """gibt minuten differenz der letzen beiden eintraege"""
                     45: 
                     46:     
                     47:     tmp=line.split("[")
                     48:     tmp=tmp[1].split("]")
                     49:     
                     50:     timestr=tmp[0]
                     51:     
                     52:     split=timestr.split(":")
                     53:     time1=int(split[1])*60+int(split[2])
                     54: 
                     55:     localtime=time.asctime(time.localtime())
                     56:     split=localtime.split(":")
                     57:     time2=int(split[0].split()[3])*60+int(split[1])
                     58: 
                     59: 
                     60:     return time2-time1
                     61:     
                     62: class checkStation(SimpleItem):
                     63:     """test ob station lebt"""
                     64: 
                     65:     meta_type="check Media Stations"
                     66: 
                     67:     def asctime(self):
                     68:         return time.asctime(tm)
                     69:         
                     70:     def __init__(self, id, path):
                     71:         """init"""
                     72:         self.path=path #pfad fuer logfile
                     73:         self.id=id
                     74:         self.modulOverview={}
                     75: 
                     76:     def checkStation(self,nr,time=5,formats=["VID"]):
                     77:         """check station with nr nr"""
                     78:         txt=[]
                     79: 
                     80:         txtFh = os.popen("grep /%s/ %s | tail "%(nr,self.path))
                     81:         for line in txtFh.readlines():
                     82:             txt.append(line)
                     83: 
                     84:         
                     85:         line=getLastLine(txt)
                     86: 
                     87:         if not line:
                     88:             return True,"",0
                     89: 
                     90:         cmpT=cmpLine(line)
                     91: 
                     92:         if not checkFormat(line,formats):
                     93:             # teste ob letzte Zeile vom format in formats
                     94: 
                     95:             return True,line,cmpT
                     96: 
                     97: 
                     98:         if cmpT > time:
                     99:             return False,line,cmpT
                    100: 
                    101:         else:
                    102:             return True,line,cmpT
                    103: 
                    104:     def getModuls(self):
                    105:         """getModuls"""
                    106:         return module_list
                    107:     def overviewStations(self,time=5):
                    108:         """overview"""
                    109:         self.REQUEST.SESSION['outTime']=time
                    110:         
                    111:         zp=zptFile(self,"zpt/overview.zpt")
                    112:         return zp()
                    113: 
                    114:     ips={"22222":"127.0.0.1","33335":"127.0.0.2"}
                    115:     
                    116:     def rebootStation(self,modul):
                    117:         """neustart"""
                    118:         ip=self.ips[modul]
                    119: 
                    120:         str="""ssh root@%s "ps -xa | grep %s" """
                    121:         rets=os.popen(str%(ip,grepString)).readlines()
                    122: 
                    123:         for ret in rets:
                    124: 
                    125:             if ret.find("grep") <= 0:
                    126:                 process=ret.split()[0]
                    127:                 str="""ssh root@%s kill -9 %s """
                    128:                 rets=os.popen(str%(ip,process)).readlines()
                    129:                 print "WW",str%(ip,process)
                    130:         
                    131:     def checkRebootDead(self,timer=5):
                    132:         """checks and reboots non responding firefox"""
                    133:         self.lastCheck=time.localtime()
                    134:         
                    135:         for modul in self.getModuls():
                    136: 
                    137:             checked=self.checkStation(modul,time=timer,formats=["VID"])
                    138:             if not checked[0]:
                    139: 
                    140:                 self.rebootStation(modul)
                    141:                 if not hasattr(self,'modulOverview'):
                    142:                     setattr(self,'modulOverview',{})
                    143:                 self.modulOverview[modul]=time.localtime()
                    144:                 
                    145: 
                    146:     def rebootOverview(self):
                    147:         """give Overview"""
                    148:         pt=zptFile(self,'zpt/rebootOverview.zpt')
                    149:         return pt()
                    150:     
                    151:     def changeCheckStationForm(self):
                    152:         """form for change"""
                    153: 
                    154:         pt=zptFile(self, 'zpt/ChangeCheckStation.zpt')
                    155: 
                    156:         return pt()
                    157: 
                    158:     def changeCheckStation(self,path,RESPONSE=None):
                    159:         """change"""
                    160:         self.path=path
                    161: 
                    162:         if RESPONSE:
                    163:             RESPONSE.redirect("manage_main")
                    164: 
                    165: 
                    166: def manage_addCheckStationForm(self):
                    167:     """add"""
                    168:     pt=zptFile(self,'zpt/AddCheckStation')
                    169:     return pt()
                    170: 
                    171: def manage_addCheckStation(self,id,path,RESPONSE=None):
                    172:     """manage checkstation"""
                    173:     
                    174:     newObj=checkStation(id,path)
                    175:     self._setObject(id,newObj)
                    176:     
                    177:     if RESPONSE is not None:
                    178:         RESPONSE.redirect('manage_main')
                    179: 

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