Пример #1
0
def clockwatcher_main():

    syslog.syslog("clockwatcherd: starting clockwatcher_main")

    lifetime = timedelta(days=1) #Notif expiration delta

    s = Signer("/etc/clockwatcher/shiny.private", "shiny")

    addrlist=[]
    updates={}
    with open("/etc/clockwatcher/clockwatcherd.cfg","r") as cfg:
        for line in cfg:
            addrlist.append(line[:-1])  #remembering to remove trailing \n

    while 1:

        # Synchronize to next whole minute
        starttime = time.localtime()
        time.sleep(60-starttime.tm_sec)

        currtime = datetime.now()+ timedelta(seconds=30) # Force rounding in case we're early
        timemsg = currtime.strftime("It is now %H:%M")
        

        notif = Notification(4, lifetime, timemsg, timemsg + " and all is well") # Need to add expiration here
        notif.prepare(s)

# For now, minimizing the possibility of a collision between this daemon and new authorizations coming in
# by reading the additional authorizations from a separate file and adding them on here. Only the daemon
# touches the main clockwatcherd.cfg file.

        rewrite = False
        try:
            with open("/etc/clockwatcher/newwatchers.cfg","r") as cfg:
                for line in cfg:
                    newaddr = line
                    if newaddr not in addrlist:  #Handle unlikely duplicates
                        addrlist.append(newaddr)
                        rewrite = True
        except IOError:
            pass
        except:
            syslog.syslog("clockwatcherd: Unknown error opening newwatchers file")
            quit()

        if rewrite:
            cfg=open("/etc/clockwatcher/newwatchers.cfg","w")  #Clobber newwatchers file
            cfg.close()

            with open("/etc/clockwatcher/clockwatcherd.cfg","w") as cfg: #Update config with new watchers
                for idx in range(len(addrlist)):
                    if addrlist[idx] != "":
                        cfg.write(addrlist[idx])
                        cfg.write("\n")
            rewrite = False

        for idx in range(len(addrlist)):
            notaddr = addrlist[idx]
            if notaddr == "":
                continue
            if notaddr in updates:  #update an existing notif if possible
                notid = updates[notaddr]
                status = notif.update(notid)
                if status == 404: #if 404 delete notid from updates
                    del updates[notaddr]
            if notaddr not in updates:  #not an else because it could have just been removed

                # TODO: Handle exceptions (can't connect, etc.) here
                (notid, status) = notif.send(notaddr) #Need to get feedback on send failures, delete notaddr
                if status == 404:
                    addrlist[idx]=""   #Don't delete entry from addrlist inside loop, just blank it
                    rewrite = True  #Disk copy of list needs updating
                elif status == 200:
                    updates[notaddr] = notid

        if rewrite:  #Update disk copy of list, removing any blank addresses
            with open("/etc/clockwatcher/clockwatcherd.cfg","w") as cfg:
                for idx in range(len(addrlist)):
                    if addrlist[idx] != "":
                        cfg.write(addrlist[idx])
                        cfg.write("\n")
Пример #2
0
def clockwatcher_main():

    syslog.syslog("clockwatcherd: starting clockwatcher_main")

    lifetime = timedelta(days=1)  #Notif expiration delta

    s = Signer("/etc/clockwatcher/shiny.private", "shiny")

    addrlist = []
    updates = {}
    with open("/etc/clockwatcher/clockwatcherd.cfg", "r") as cfg:
        for line in cfg:
            addrlist.append(line[:-1])  #remembering to remove trailing \n

    while 1:

        # Synchronize to next whole minute
        starttime = time.localtime()
        time.sleep(60 - starttime.tm_sec)

        currtime = datetime.now() + timedelta(
            seconds=30)  # Force rounding in case we're early
        timemsg = currtime.strftime("It is now %H:%M")

        notif = Notification(4, lifetime, timemsg, timemsg +
                             " and all is well")  # Need to add expiration here
        notif.prepare(s)

        # For now, minimizing the possibility of a collision between this daemon and new authorizations coming in
        # by reading the additional authorizations from a separate file and adding them on here. Only the daemon
        # touches the main clockwatcherd.cfg file.

        rewrite = False
        try:
            with open("/etc/clockwatcher/newwatchers.cfg", "r") as cfg:
                for line in cfg:
                    newaddr = line
                    if newaddr not in addrlist:  #Handle unlikely duplicates
                        addrlist.append(newaddr)
                        rewrite = True
        except IOError:
            pass
        except:
            syslog.syslog(
                "clockwatcherd: Unknown error opening newwatchers file")
            quit()

        if rewrite:
            cfg = open("/etc/clockwatcher/newwatchers.cfg",
                       "w")  #Clobber newwatchers file
            cfg.close()

            with open("/etc/clockwatcher/clockwatcherd.cfg",
                      "w") as cfg:  #Update config with new watchers
                for idx in range(len(addrlist)):
                    if addrlist[idx] != "":
                        cfg.write(addrlist[idx])
                        cfg.write("\n")
            rewrite = False

        for idx in range(len(addrlist)):
            notaddr = addrlist[idx]
            if notaddr == "":
                continue
            if notaddr in updates:  #update an existing notif if possible
                notid = updates[notaddr]
                status = notif.update(notid)
                if status == 404:  #if 404 delete notid from updates
                    del updates[notaddr]
            if notaddr not in updates:  #not an else because it could have just been removed

                # TODO: Handle exceptions (can't connect, etc.) here
                (notid, status) = notif.send(
                    notaddr
                )  #Need to get feedback on send failures, delete notaddr
                if status == 404:
                    addrlist[
                        idx] = ""  #Don't delete entry from addrlist inside loop, just blank it
                    rewrite = True  #Disk copy of list needs updating
                elif status == 200:
                    updates[notaddr] = notid

        if rewrite:  #Update disk copy of list, removing any blank addresses
            with open("/etc/clockwatcher/clockwatcherd.cfg", "w") as cfg:
                for idx in range(len(addrlist)):
                    if addrlist[idx] != "":
                        cfg.write(addrlist[idx])
                        cfg.write("\n")