示例#1
0
        def __init__(self,INI):                
                self.log = lib_Log.Log(PrintToConsole=True)
                self.Error = False
                self.INI = INI
                self.ReadError = False
                self.Renew = False
                self.ClearFailoverVariables()
                self.ServerIP = "185.95.73.13"
                self.ServerVPNIP = "185.95.73.13"
                self.Ping = lib_ICMP.Ping(self.ServerIP)
                self.PingVPN = lib_ICMP.Ping(self.ServerVPNIP)

                self.StandardDefaultGW = self.GetDefaultGateway()

                #create variables for priorities (datacounter)
                self.CounterDatatypes = ("rx_bytes","tx_bytes")
                self.CounterInterfaces = "tun1"
                temp = lib_LocalInterface.ReadBandwith(datatypes = self.CounterDatatypes, interfaces = self.CounterInterfaces)
                self.rxBytes = temp["tun1"]["rx_bytes"]
                self.txBytes = temp["tun1"]["tx_bytes"]
                self.MailInternalSend = False
                self.MailExternalSend = False
                ExternalMailDelay    = self.INI.getOptionInt("FAILOVERGENERAL","EXTERNALMAILDELAY")
                if ExternalMailDelay is None:
                        #if no delay is set, we set to 30 min
                        ExternalMailDelay = 60 * 30
                        self.log.printWarning("ExternalMailDelay not defined in ini-file, set to %s seconds" % ExternalMailDelay)
                InternalMailDelay    = self.INI.getOptionInt("FAILOVERGENERAL","INTERNALMAILDELAY")
                if InternalMailDelay is None:
                        #if no delay is set, we set to 15 min
                        InternalMailDelay = 60 * 15
                        self.log.printWarning("InternalMailDelay not defined in ini-file, set to %s seconds" % InternalMailDelay)
                self.ExternalMailDelay = ExternalMailDelay
                self.InternalMailDelay = InternalMailDelay
                self.ExternalMailSend = False
                self.InternalMailSend = False
                self.ChangedInterfaceTime = None
                self.InterfaceBeforeLastMail = None
                self.LastSuccessVPNPing = time.time()
                #how long does an iterface deserves to be "burned" before we can use it again (seconds)
                self.InterfaceBurnRestoreTime = 60 * 10
                #after how long with unsuccessfull vpn pings an interface becomes "burned"
                self.InterfaceBurnTime = 60 * 1
                #initialize time of burn to now, so counter starts here
                self.InterfaceBurningTime = time.time()
                self.BurnedInterface = None
                #checktimes is used for how often we need to check some parameters
                self.checktime = time.time()


                #create SQL instance
                # self.sql = lib_SQLdb.Database()
                self.couch_db = lib_CouchDB.COUCH_DATABASE()

                config_data = lib_config.CONFIG_DATA()
                
                self.config_interface = config_data.interface
示例#2
0
    def OpenConnection(self, IP="192.168.0.3", Port=23):
        """
                Opens  a Telnet  connection  if  not  already  open
                """
        if self.Error == False:
            PING = lib_ICMP.Ping(IP)
            if PING.DoPingTest():
                self.log.printInfo("Successfully Reached %s" % IP)
            else:
                self.log.printWarning("Can Not Reach %s" % IP)
                self.Error = True

        if self.Error == False:
            '''
                        tn.read_until("login: "******"\n")
                        if password:
                            tn.read_until("Password: "******"\n")

                        '''

            try:
                self.Telnet = telnetlib.Telnet(IP, Port, self.TelenetTimeout)
                self.log.printInfo("Telnet Connecting")
                self.Telnet_open = True
            except:
                self.log.printError(
                    "%s TELNET Connection Failed, Please Check Credentials" %
                    sys._getframe().f_code.co_name)
                self.Error = True
        return self.Error
示例#3
0
    def OpenConnection(self,
                       Username="******",
                       Password="******",
                       IP="192.168.0.3",
                       Port=22):
        """
                Opens  a SSH  connection  if  not  already  open
                """

        if self.Error == False:
            PING = lib_ICMP.Ping(IP)
            if PING.DoPingTest():
                self.log.printInfo("Successfully Reached %s" % IP)
            else:
                self.log.printWarning("Can Not Reach %s" % IP)
                self.Error = True

        if self.Error == False:
            self.log.printInfo("Connecting SSH")

            try:
                self.SSH.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                self.SSH.connect(IP,
                                 username=Username,
                                 password=Password,
                                 port=int(Port),
                                 timeout=50)
                self.SSH_open = True
                self.log.printInfo("SSH Connected")
            except:
                self.log.printError(
                    "%s SSH Connection Failed, Please Check Credentials" %
                    sys._getframe().f_code.co_name)
                self.Error = True
        return self.Error
示例#4
0
    def URLWithAuthenticate(self, Username, Password, URL):

        if self.Error == False:
            IP = re.findall("\/\/([^\/]+)\/", URL)[0]
            IP = IP.split(':')[0]
            PING = lib_ICMP.Ping(IP)
            if PING.DoPingTest():
                self.log.printInfo("Successfully Reached %s" % IP)
            else:
                self.log.printWarning("Can Not Reach %s" % IP)
                self.Error = True

        if self.Error == False:
            try:
                data = requests.get(URL,
                                    auth=HTTPDigestAuth(Username, Password),
                                    timeout=10)
                self.soup = BeautifulSoup(data.content, "html.parser")

            except Exception as e:
                self.soup = ""
示例#5
0
    def GetData(self):
        if self.Error == False:
            try:
                result = None

                pingAdresses = self.INI.getOption(self.deviceDescr,
                                                  self.devNumber, "IP")

                if pingAdresses is not None:
                    result = {}
                    for key in pingAdresses:
                        adress = pingAdresses[key].strip()
                        pingTest = lib_ICMP.Ping(adress)
                        result[adress] = pingTest.DoPingTestExtended()

                        if (result[adress]["error"] == True):
                            self.log.printInfo("Ping failed - %s" % adress)
                        else:
                            self.log.printInfo("Ping succesfull - %s" % adress)

                #        sql = lib_SQLdb.Database()
                #Device = "%s%s" % (self.deviceDescr, self.devNumber)
                #sql.Create_General_Option_Module(self.INI,result,Device, ModuleOption="dest")
                sqlArray = {}
                sqlArray[self.deviceDescr] = {}
                sqlArray[self.deviceDescr][self.devNumber] = {}
                sqlArray[self.deviceDescr][self.devNumber] = result
                sqlArray[self.deviceDescr][self.devNumber]["_ExtractInfo"] = {}
                sqlArray[self.deviceDescr][self.devNumber]["_ExtractInfo"][
                    "ExtractTime"] = time.time()
                sqlArray["ReadError"] = False
                return sqlArray

            except Exception as e:
                self.log.printError("%s Module Error" %
                                    sys._getframe().f_code.co_name)
                self.log.printError(str(e))
                self.Error = True
                return None
示例#6
0
    def URL(self, URL):

        if self.Error == False:
            IP = re.findall("\/\/([^\/]+)\/", URL)[0]
            IP = IP.split(':')[0]
            PING = lib_ICMP.Ping(IP)

            if PING.DoPingTest():
                self.log.printInfo("Successfully Reached %s" % IP)
            else:
                self.log.printWarning("Can Not Reach %s" % IP)
                self.Error = True

        if self.Error == False:
            try:
                if URL.lower().startswith("https"):
                    context = ssl._create_unverified_context()
                    page = urllib2.urlopen(URL, timeout=10, context=context)
                else:
                    page = urllib2.urlopen(URL, timeout=10)
                self.soup = BeautifulSoup(page, "html.parser")
            except Exception as e:
                self.Error = True
                self.log.printError(e)
示例#7
0
        def OwnFailover(self):
                #Remove old and set the new Gateways with metric. Included DHCP with metric 100
                self.SetInitalGateways(StartMetric = 10)
                #initialize
                self.TweenTime = time.time()   
                mainLoopTime    = self.INI.getOptionInt("MAIN_PROGRAM","MAINLOOPTIME")
                if mainLoopTime is None:
                        mainLoopTime = 60 * 10
                        self.log.printWarning("Main loop time time not defined in ini-file, set to %s seconds" % mainLoopTime)
                #Create ping instance towards VPN server
                Ping = lib_ICMP.Ping(self.ServerIP)
                self.Policy = None                
                
                #forever loop
                while True:
                        try:
                                #make sure failover is not taking all recources from processor
                                time.sleep(2)
                                #Renew info when Main reInitializes ini and sets self.Renew or on first run
                                if self.Renew:
                                        self.log.printInfo("Reloading Failover Parameters dus to INI files updates")
                                        self.Priorities = self.GetFailoverPriorities()
                                        #Remove old and set the new Gateways with metric. Included DHCP with metric 100
                                        self.SetInitalGateways(StartMetric = 10)
                                        #set all counter to NOW (Zero)
                                        self.ClearFailoverVariables()
                                        self.interface = None

                                        self.TweenTime = time.time()   
                                        mainLoopTime    = self.INI.getOptionInt("MAIN_PROGRAM","MAINLOOPTIME")
                                        if mainLoopTime is None:
                                                mainLoopTime = 60 * 10
                                                self.log.printWarning("Main loop time time not defined in ini-file, set to %s seconds" % mainLoopTime)
                                        ExternalMailDelay    = self.INI.getOptionInt("FAILOVERGENERAL","EXTERNALMAILDELAY")
                                        if ExternalMailDelay is None:
                                                #if no delay is set, we set to 15 min
                                                ExternalMailDelay = 60 * 15
                                                self.log.printWarning("ExternalMailDelay not defined in ini-file, set to %s seconds" % ExternalMailDelay)
                                        InternalMailDelay    = self.INI.getOptionInt("FAILOVERGENERAL","EXTERNALMAILDELAY")
                                        if InternalMailDelay is None:
                                                #if no delay is set, we set to 25 min
                                                InternalMailDelay = 60 * 25
                                                self.log.printWarning("InternalMailDelay not defined in ini-file, set to %s seconds" % InternalMailDelay)
                                        self.ExternalMailDelay = ExternalMailDelay
                                        self.InternalMailDelay = InternalMailDelay
                                        self.ExternalMailSend = False
                                        self.InternalMailSend = False
                                        self.ChangedInterfaceTime = None
                                        self.Renew = False

                                #Every looptime, confirm all  data to SQL
                                if (time.time() - self.TweenTime) >= mainLoopTime:
                                        #writing to database
                                        self.WriteToDatabase()
                                        self.TweenTime = time.time()

                                #check all every 5 sec
                                if ((self.checktime + 10) < time.time()):
                                        self.checktime = time.time()
                                        #check if mailsend is required
                                        self.SendMailIfRequired()

                                        #check if VPN is UP
                                        self.CheckVPN()
                                
                                for a in self.Priorities:
                                        testInterface = self.INI.getOptionStr("FAILOVER",a,"INTERFACE")
                                        # print "testinterface = %s" % testInterface
                                        if ((testInterface != None) and (self.BurnedInterface != testInterface)):
                                                retry = 0
                                                pingtime = time.time()
                                                while retry < 5:
                                                        if time.time() > (pingtime + 10) :
                                                                pingtime = time.time()
                                                                # print "retry = %s" % retry
                                                                # print "failover alive"
                                                                # print "ping %s" % testInterface
                                                                # print "\n"
                                                                #wait time between ping tries
                                                                # time.sleep(10)
                                                                #get average success ping result and if better than 60% continue (3 out of 5)
                                                                successRate = Ping.GetPingSuccessrate(pingAmount = 5,interface = testInterface)
                                                                # print "successRate = %s" % successRate
                                                                if successRate >= 0.6:
                                                                        #if this is priority 1 at boot 
                                                                        if self.interface == None and a == min(self.Priorities):
                                                                                self.ReplaceMainGateway(FailoverNumber = a)
                                                                                # no mail send is required, but print to log is
                                                                                self.log.printOK("System went to Preferred Network '%s' at STARTUP" % self.Description)
                                                                        elif testInterface != self.interface:
                                                                                #change inteface and store values
                                                                                self.ReplaceMainGateway(FailoverNumber = a)
                                                                                #prepare message for mail and write to log
                                                                                self.SetMessageChangeInterface(FailoverNumber = a)
                                                                        else:
                                                                                pass
                                                                                #self.log.printInfo("Interface %s was already set as Default Gateway"% self.interface)
                                                                                print ("Interface %s was already set as Default Gateway" % self.interface)
                                                                        raise ValueError("restart ping cycle")
                                                                else:
                                                                        pass
                                                                        #self.log.printError("Ping via %s Failed" % self.interface)
                                                                        #print "Ping via %s Failed" % self.interface
                                                                retry = retry + 1
                                                        else:
                                                                time.sleep(1)

                                #if program gets here, the default interface could not be set.
                                #We can try to go to the default gateway given by the dhcp lease
                                if self.StandardDefaultGW != None:
                                        testInterface = self.config_interface
                                        # print "testinterface = %s" % testInterface
                                        retry = 0
                                        while retry < 5:
                                                #wait time between ping tries
                                                if time.time() > (pingtime + 10) :
                                                                pingtime = time.time()
                                                                # print "retry = %s" % retry
                                                                retry = retry + 1
                                                                #get average success ping result and if better than 60% continue (3 out of 5)
                                                                if Ping.GetPingSuccessrate(pingAmount = 5,interface = testInterface) >= 0.6:
                                                                        if testInterface != self.interface:
                                                                                self.ReplaceMainGateway(DHCP = True)
                                                                                self.SetMessageChangeInterface(DHCP = True)                
                                                                                raise ValueError("restart ping cycle")
                        except KeyboardInterrupt:
                                self.log.printError ("\nMain Program stopped by User (CTRL+C)")     
                                exit()
                        except Exception as e:
                                # print "error"
                                # print e
                                # print "\n"
                                pass
示例#8
0
        def ExternalFailover(self):
                CurrentInterface = None
                #tracert = lib_ICMP.Ping("8.8.8.8")
                tracert = lib_ICMP.Ping(self.ServerIP)
                #create SQL instance
                # sql = lib_SQLdb.Database()
                #set all counter to NOW (Zero)
                MaxHops = self.INI.getOptionInt("FAILOVERGENERAL","MAXHOPS")
                if MaxHops == None:
                        self.log.printWarning("Amount of MaxHops not defined in INI, MaxHops is set to 5 by default")
                        MaxHops = 5
                Gateway = None
                interface = None
                Description = None
                result = {}
                result["Gateway"] = Gateway
                TweenTime = time.time()   
                mainLoopTime    = self.INI.getOptionInt("MAIN_PROGRAM","MAINLOOPTIME")
                if mainLoopTime is None:
                        mainLoopTime = 60 * 10
                        self.log.printWarning("Main loop time time not defined in ini-file, set to %s seconds" % mainLoopTime)

                while True:
                        try:
                                #Renew info when Main reInitializes ini and sets self.Renew
                                if self.Renew:
                                        CurrentInterface = None
                                        Gateway = None
                                        interface = None
                                        Priority = None
                                        Description = None
                                        self.Policy = None
                                        self.Renew = False
                                        MaxHops = self.INI.getOptionInt("FAILOVERGENERAL","MAXHOPS")
                                        if MaxHops == None:
                                                MaxHops = 5
                                        ExternalMailDelay    = self.INI.getOptionInt("FAILOVERGENERAL","EXTERNALMAILDELAY")
                                        if ExternalMailDelay is None:
                                                #if no delay is set, we set to 30 min
                                                ExternalMailDelay = 60 * 30
                                                self.log.printWarning("ExternalMailDelay not defined in ini-file, set to %s seconds" % ExternalMailDelay)
                                        InternalMailDelay    = self.INI.getOptionInt("FAILOVERGENERAL","EXTERNALMAILDELAY")
                                        if InternalMailDelay is None:
                                                #if no delay is set, we set to 15 min
                                                InternalMailDelay = 60 * 15
                                                self.log.printWarning("InternalMailDelay not defined in ini-file, set to %s seconds" % InternalMailDelay)
                                        self.ExternalMailDelay = ExternalMailDelay
                                        self.InternalMailDelay = InternalMailDelay



                                #Every looptime, confirm all  data to SQL
                                if (time.time() - TweenTime) >= mainLoopTime:
                                        TweenTime = time.time()
                                        result = {}
                                        result["Description"] = Description
                                        result["Gateway"] = Gateway
                                        result["Interface"] = interface
                                        result["Policy"] = self.Policy
                                        result["Failover"] = Priority
                                        result.update(self.GetCounters())
                                        #create tables, fill tables
                                        paramArray = {}
                                        paramArray["FAILOVER"] = {}
                                        paramArray["FAILOVER"][0] = {}
                                        paramArray["FAILOVER"][0]["General"] = result
                                        paramArray["FAILOVER"][0]["_ExtractInfo"] = {}
                                        paramArray["FAILOVER"][0]["_ExtractInfo"]["ExtractTime"] = time.time()
                                        
                                        print "1============================================="
                                        print "Class_Failover"
                                        print "1============================================="
                                        # sql.WriteArrayData(self.INI,paramArray,uniqueTable = "failover")
                                        self.couch_db.write_default_values(self.INI, paramArray, uniqueTable = "failover")
                                        print "1============================================="
                                        print "END"
                                        print "1============================================="
                                

                                Hop=1
                                Continue = True
                                while Continue:
                                        if Hop > MaxHops:
                                                self.log.printWarning("MaxHops '%s' is reached without resolving a known/defined gateway")
                                                Hop = 1
                                        traceresult = tracert.DoTraceroute(Hops = Hop)
                                        Hop += 1
                                        if traceresult != None:
                                                Gateway = traceresult[max(traceresult)]["IP"]
                                                KnownGateway = False
                                                for a in Priorities:
                                                        if Gateway == self.INI.getOptionStr("FAILOVER",a,"GATEWAY"):
                                                                Description = self.INI.getOptionStr("FAILOVER",a,"DESCRIPTION")
                                                                self.Policy = self.INI.getOptionStr("FAILOVER",a,"POLICY")
                                                                interface = self.INI.getOptionStr("FAILOVER",a,"INTERFACE")
                                                                Priority = "FAILOVER" + str(a)
                                                                Continue = False
                                                                KnownGateway = True
                                                                break

                                                if Gateway != result["Gateway"] and KnownGateway == True:
                                                        if a == min(Priorities) and result["Gateway"] == None:
                                                                Message = "System went to Preferred Network '%s' at STARTUP" % Description
                                                                self.log.printOK(Message)
                                                        else:
                                                                if a == min(Priorities):
                                                                        Message = "System returned to preferred Network '%s'" % Description                 
                                                                else:
                                                                        Message = "Failover switched to '%s'. This profile has priority %s. \n"%(Description,a)
                                                                        Message += "\nYour prefered profile '%s' has no internet connection.\n" % (self.INI.getOptionStr("FAILOVER",min(Priorities),"DESCRIPTION"))
                                                                        for backup in Priorities:
                                                                                if backup != min(Priorities) and backup < a:
                                                                                        Message += "Profile '%s' with priority %s, has also no internet connection.\n" % (self.INI.getOptionStr("FAILOVER",backup,"DESCRIPTION"),backup) 
                                                                        Message += "\nPlease be aware of the change as it may result in expencive connectivity bills.\n"

                                                                Message += "\n\nThis is an autogenerated Message, pls do not reply!\n"
                                                                Message += "\n\nBest Regards, \nRHBOX-Team"
                                                                Message += "\n\n--------------------------------------------------"
                                                                Message += "\nMailTimestamp: " + str(time.strftime("%Y-%m-%d %H:%M:%S")) + " UTC"


                                                                #print Message
                                                                Mail = lib_Mail.Mail(self.INI,"FAILOVERGENERAL")
                                                                Mail.Set_Subject("Failover Event,  %s   IMO: %s   MMSI: %s" % (self.INI.getOptionStr("INFO","VESSELNAME"),self.INI.getOptionStr("INFO","IMO"),self.INI.getOptionStr("INFO","MMSI")))
                                                                Mail.Set_Message(Message)
                                                                #Mail.Set_Attachement("/rhbox/log/","VSATBOX.log")
                                                                Mail.Send_Mail()
                                                                del Mail

                                                        #write the results into Database
                                                        TweenTime = time.time()
                                                        result = {}
                                                        result["Description"] = Description
                                                        result["Gateway"] = Gateway
                                                        result["Interface"] = interface
                                                        result["Policy"] = self.Policy
                                                        result["Failover"] = Priority
                                                        result.update(self.GetCounters())
                                                        self.log.printInfo("Failover set to '%s' GW '%s' IF '%s' with Policy '%s'"% (Description,Gateway,interface,self.Policy))
                                                        #create tables, fill tables
                                                        paramArray = {}
                                                        paramArray["FAILOVER"] = {}
                                                        paramArray["FAILOVER"][0] = {}
                                                        paramArray["FAILOVER"][0]["General"] = result
                                                        paramArray["FAILOVER"][0]["_ExtractInfo"] = {}
                                                        paramArray["FAILOVER"][0]["_ExtractInfo"]["ExtractTime"] = time.time()
                                                        
                                                        print "2============================================="
                                                        print "Class_Failover"
                                                        print "2============================================="
                                                        # sql.WriteArrayData(self.INI,paramArray,uniqueTable = "failover")
                                                        self.couch_db.write_default_values(self.INI, paramArray, uniqueTable = "failover")
                                                        print "2============================================="
                                                        print "END"
                                                        print "2============================================="
                                #wait time between ping tries
                                time.sleep(10)

                        except KeyboardInterrupt:
                                self.log.printError ("\nMain Program stopped by User (CTRL+C)")     
                                exit()
                        except Exception as e:
                                pass