def StartThreads(self, reload=False): try: # start thread to accept incoming sockets for nagios heartbeat self.Threads["ComWatchDog"] = mythread.MyThread(self.ComWatchDog, Name="ComWatchDog") if self.bSyncDST or self.bSyncTime: # Sync time thread self.Threads["TimeSyncThread"] = mythread.MyThread( self.TimeSyncThread, Name="TimeSyncThread") if not self.DisableWeather and not self.WeatherAPIKey == None and len( self.WeatherAPIKey ) and not self.WeatherLocation == None and len( self.WeatherLocation): Unit = 'metric' if self.UseMetric else 'imperial' self.MyWeather = myweather.MyWeather( self.WeatherAPIKey, location=self.WeatherLocation, unit=Unit, log=self.log) self.Threads = self.MergeDicts(self.Threads, self.MyWeather.Threads) except Exception as e1: self.LogErrorLine("Error in StartThreads: " + str(e1))
def StartTimeThread(self): # This is done is a separate thread as not to block any return email processing # since we attempt to sync with generator time mythread.MyThread(self.Controller.SetGeneratorTimeDate, Name="SetTimeThread") return "Time Set: Command Sent\n"
def __init__(self, host="127.0.0.1", port=9082, log=None, callback=None, polltime=None, blacklist=None, flush_interval=float('inf'), use_numeric=False, debug=False): super(MyGenPush, self).__init__() self.Callback = callback self.UseNumeric = use_numeric self.Debug = debug if polltime == None: self.PollTime = 3 else: self.PollTime = float(polltime) if log != None: self.log = log else: # log errors in this module to a file self.log = mylog.SetupLogger("client", "/var/log/mygenpush.log") self.console = mylog.SetupLogger("mygenpush_console", log_file="", stream=True) self.AccessLock = threading.Lock() self.BlackList = blacklist self.LastValues = {} self.FlushInterval = flush_interval self.LastChange = {} try: startcount = 0 while startcount <= 10: try: self.Generator = myclient.ClientInterface(host=host, log=log) break except Exception as e1: startcount += 1 if startcount >= 10: self.console.info("genmon not loaded.") self.LogError("Unable to connect to genmon.") sys.exit(1) time.sleep(1) continue # start thread to accept incoming sockets for nagios heartbeat self.Threads["PollingThread"] = mythread.MyThread( self.MainPollingThread, Name="PollingThread") except Exception as e1: self.LogErrorLine("Error in mygenpush init: " + str(e1))
def StartThreads(self, reload=False): # start thread to accept incoming sockets for nagios heartbeat and command / status clients self.Threads["InterfaceServerThread"] = mythread.MyThread( self.InterfaceServerThread, Name="InterfaceServerThread") # start thread to accept incoming sockets for nagios heartbeat self.Threads["ComWatchDog"] = mythread.MyThread(self.ComWatchDog, Name="ComWatchDog") if self.bSyncDST or self.bSyncTime: # Sync time thread self.Threads["TimeSyncThread"] = mythread.MyThread( self.TimeSyncThread, Name="TimeSyncThread") if not self.WeatherAPIKey == None and len( self.WeatherAPIKey ) and not self.WeatherLocation == None and len(self.WeatherLocation): Unit = 'metric' if self.UseMetric else 'imperial' self.MyWeather = myweather.MyWeather(self.WeatherAPIKey, location=self.WeatherLocation, unit=Unit, log=self.log) self.Threads = self.MergeDicts(self.Threads, self.MyWeather.Threads)
def __init__(self, ConfigFilePath=None): super(Monitor, self).__init__() self.ProgramName = "Generator Monitor" self.Version = "Unknown" self.log = None self.IsStopping = False self.ProgramComplete = False if ConfigFilePath == None: self.ConfigFilePath = "/etc/" else: self.ConfigFilePath = ConfigFilePath self.ConnectionList = [] # list of incoming connections for heartbeat # defautl values self.SiteName = "Home" self.ServerSocket = None self.ServerSocketPort = 9082 # server socket for nagios heartbeat and command/status self.IncomingEmailFolder = "Generator" self.ProcessedEmailFolder = "Generator/Processed" self.FeedbackLogFile = os.path.dirname( os.path.realpath(__file__)) + "/feedback.json" self.LogLocation = "/var/log/" self.LastLogFileSize = 0 self.NumberOfLogSizeErrors = 0 # set defaults for optional parameters self.NewInstall = False # True if newly installed or newly upgraded version self.FeedbackEnabled = False # True if sending autoated feedback on missing information self.FeedbackMessages = {} self.MailInit = False # set to true once mail is init self.CommunicationsActive = False # Flag to let the heartbeat thread know we are communicating self.Controller = None self.ControllerSelected = None self.bDisablePlatformStats = False self.ReadOnlyEmailCommands = False self.SlowCPUOptimization = False # weather parameters self.WeatherAPIKey = None self.WeatherLocation = None self.UseMetric = False self.WeatherMinimum = True self.DisableWeather = False self.MyWeather = None # Time Sync Related Data self.bSyncTime = False # Sync gen to system time self.bSyncDST = False # sync time at DST change self.bDST = False # Daylight Savings Time active if True # simulation self.Simulation = False self.SimulationFile = None self.console = mylog.SetupLogger("genmon_console", log_file="", stream=True) if os.geteuid() != 0: self.LogConsole( "You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'." ) sys.exit(1) if not os.path.isfile(self.ConfigFilePath + 'genmon.conf'): self.LogConsole("Missing config file : " + self.ConfigFilePath + 'genmon.conf') sys.exit(1) if not os.path.isfile(self.ConfigFilePath + 'mymail.conf'): self.LogConsole("Missing config file : " + self.ConfigFilePath + 'mymail.conf') sys.exit(1) # log errors in this module to a file self.log = mylog.SetupLogger("genmon", self.LogLocation + "genmon.log") self.config = myconfig.MyConfig(filename=self.ConfigFilePath + 'genmon.conf', section="GenMon", log=self.console) # read config file if not self.GetConfig(): self.LogConsole("Failure in Monitor GetConfig") sys.exit(1) # log errors in this module to a file self.log = mylog.SetupLogger("genmon", self.LogLocation + "genmon.log") self.config.log = self.log if self.IsLoaded(): self.LogConsole("ERROR: genmon.py is already loaded.") self.LogError("ERROR: genmon.py is already loaded.") sys.exit(1) if self.NewInstall: self.LogError("New version detected: Old = %s, New = %s" % (self.Version, GENMON_VERSION)) self.Version = GENMON_VERSION self.ProgramStartTime = datetime.datetime.now() # used for com metrics atexit.register(self.Close) signal.signal(signal.SIGTERM, self.Close) signal.signal(signal.SIGINT, self.Close) # start thread to accept incoming sockets for nagios heartbeat and command / status clients self.Threads["InterfaceServerThread"] = mythread.MyThread( self.InterfaceServerThread, Name="InterfaceServerThread") # init mail, start processing incoming email self.mail = mymail.MyMail(monitor=True, incoming_folder=self.IncomingEmailFolder, processed_folder=self.ProcessedEmailFolder, incoming_callback=self.ProcessCommand) self.Threads = self.MergeDicts(self.Threads, self.mail.Threads) self.MailInit = True self.FeedbackPipe = mypipe.MyPipe("Feedback", self.FeedbackReceiver, log=self.log) self.Threads = self.MergeDicts(self.Threads, self.FeedbackPipe.Threads) self.MessagePipe = mypipe.MyPipe("Message", self.MessageReceiver, log=self.log, nullpipe=self.mail.DisableSNMP) self.Threads = self.MergeDicts(self.Threads, self.MessagePipe.Threads) try: #Starting device connection if self.Simulation: self.LogError("Simulation Running") if not self.ControllerSelected == None and len( self.ControllerSelected): self.LogError("Selected Controller: " + str(self.ControllerSelected)) else: self.ControllerSelected = "generac_evo_nexus" if self.ControllerSelected.lower() == "h_100": self.Controller = generac_HPanel.HPanel( self.log, newinstall=self.NewInstall, simulation=self.Simulation, simulationfile=self.SimulationFile, message=self.MessagePipe, feedback=self.FeedbackPipe, config=self.config) else: self.Controller = generac_evolution.Evolution( self.log, self.NewInstall, simulation=self.Simulation, simulationfile=self.SimulationFile, message=self.MessagePipe, feedback=self.FeedbackPipe, config=self.config) self.Threads = self.MergeDicts(self.Threads, self.Controller.Threads) except Exception as e1: self.FatalError("Error opening controller device: " + str(e1)) return None self.StartThreads() self.ProcessFeedbackInfo() # send mail to tell we are starting self.MessagePipe.SendMessage( "Generator Monitor Starting at " + self.SiteName, "Generator Monitor Starting at " + self.SiteName, msgtype="info") self.LogError("GenMon Loaded for site: " + self.SiteName)