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