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, 10 months ago) by dwinter
Branches: MAIN
CVS tags: HEAD
minor

from OFS.SimpleItem import SimpleItem
from Globals import package_home

from Products.PageTemplates.PageTemplateFile import PageTemplateFile
import os
import os.path
import time

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']

# station non checked: 31211

grepString="firefox-bin"

def zptFile(self, path, orphaned=False):
    """returns a page template file from the product"""
    if orphaned:
	# unusual case
	pt=PageTemplateFile(os.path.join(package_home(globals()), path))
    else:
	pt=PageTemplateFile(os.path.join(package_home(globals()), path)).__of__(self)
    return pt


def getLastLine(lines):
    """letzte"""
    length=len(lines)

    if length>0:
        return lines[length-1]
    else:
        return None

def checkFormat(line,formats):
    """checke formats"""
    
    splitted=line.split("GET")
    splitted=splitted[1].split()
    
    splitted2=splitted[0].split("/")

    try:
     if splitted2[4][0:3] in formats:
        return True
     else:
        return False
    except:
        return False

def getIP(line):
    """ip aus pound log line"""
    splitted=line.split()
    try:
        return splitted[5]
    except:
        return 0
    
def cmpLine(line):
    """gibt minuten zwischen letztem Eintrag und localtime"""

    # Z2.log
    ## tmp=line.split("[")
##     tmp=tmp[1].split("]")
    
##     timestr=tmp[0]
    
##     

    #pound log
    tmp=line.split()
    timestr=tmp[2]
    
    
    split=timestr.split(":")
    time1=int(split[0])*60+int(split[1])

    localtime=time.asctime(time.localtime())
    
    split=localtime.split(":")
    time2=int(split[0].split()[3])*60+int(split[1])


    return time2-time1
    
class checkStation(SimpleItem):
    """test ob station lebt"""

    meta_type="check Media Stations"

    manage_options=SimpleItem.manage_options+(
		{'label':'Overview Stations','action':'overviewStations'},
                {'label':'change check','action':'changeCheckStationForm'},
                {'label':'checkRebootDead','action':'checkRebootDead'},
                {'label':'Reboot Overview','action':'rebootOverview'},
		)
    def asctime(self,tm):
	try:
        	return time.asctime(tm)
        except:
		return "never"

    def __init__(self, id, path):
        """init"""
        self.path=path #pfad fuer logfile
        self.id=id
        self.modulOverview={}

    def checkStation(self,nr,time=7,formats=["VID","BRA","ZOG","TXT","IMG"]):
        """check station with nr nr"""
        txt=[]

        txtFh = os.popen("grep /%s/ %s | tail "%(nr,self.path))
        for line in txtFh.readlines():
            txt.append(line)

        
        line=getLastLine(txt)

        if not line:
            return True,"",0

        cmpT=cmpLine(line)
        ip=getIP(line)
        
        if not checkFormat(line,formats):
            # teste ob letzte Zeile vom format in formats

            return True,line,cmpT,ip


        if cmpT > time:
            return False,line,cmpT,ip

        else:
            return True,line,cmpT,ip

    def getModuls(self):
        """getModuls"""
        return module_list
    def overviewStations(self,time=5):
        """overview"""
        self.REQUEST.SESSION['outTime']=time
        
        zp=zptFile(self,"zpt/overview.zpt")
        return zp()

    ips={"22222":"127.0.0.1","33335":"127.0.0.2"}
    
    def rebootStation(self,ip):
        """neustart"""
        retStr=""
        #ip=self.ips[modul]

        str="""ssh root@%s "ps -xa | grep %s" """
        rets=os.popen(str%(ip,grepString)).readlines()

        for ret in rets:

            if ret.find("grep") <= 0:
                process=ret.split()[0]
                str="""ssh root@%s kill -9 %s """
                os.popen(str%(ip,process)).readlines()
                retStr+=str%(ip,process) + "<br>"
        return retStr
        
    def checkRebootDead(self,timer=7):
        """checks and reboots non responding firefox"""
        self.lastCheck=time.localtime()
        ret="<html><body><h1>check and reboot</h1><p>"
        
        for modul in self.getModuls():

            checked=self.checkStation(modul,time=timer)
            if not checked[0]:

                ret+=self.rebootStation(checked[3])
                if not hasattr(self,'modulOverview'):
                    setattr(self,'modulOverview',{})
                self.modulOverview[modul]=time.localtime()
	
        return ret+"</p></body></html>"

    def rebootOverview(self):
        """give Overview"""
        pt=zptFile(self,'zpt/rebootOverview.zpt')
        return pt()
    
    def changeCheckStationForm(self):
        """form for change"""

        pt=zptFile(self, 'zpt/ChangeCheckStation.zpt')

        return pt()

    def changeCheckStation(self,path,RESPONSE=None):
        """change"""
        self.path=path

        if RESPONSE:
            RESPONSE.redirect("manage_main")


def manage_addCheckStationForm(self):
    """add"""
    pt=zptFile(self,'zpt/AddCheckStation')
    return pt()

def manage_addCheckStation(self,id,path,RESPONSE=None):
    """manage checkstation"""
    
    newObj=checkStation(id,path)
    self._setObject(id,newObj)
    
    if RESPONSE is not None:
        RESPONSE.redirect('manage_main')


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