File:  [Repository] / checkStation / checkStation.py
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Thu Jun 23 17:12:56 2005 UTC (18 years, 11 months ago) by dwinter
Branches: MAIN
CVS tags: HEAD
minor

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

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