Example #1
0
def main(serv, port):
	port = str(port)
	firstrun = True
	currentstat = []
	readstats = ""
	killer = GracefulKiller()
	
	while True:
		try:
			readstats = subprocess.check_output(["echo show stat | nc "+serv+" "+port], shell=True)
		except:
			print("Unexpected error:", sys.exc_info())
			sys.exit()
			
		vips = readstats.splitlines()
		
		for i in range(0,len(vips)):
			if "UP" in str(vips[i]):		 			
				currentstat.append("UP")
			elif "DOWN" in str(vips[i]):
				currentstat.append("DOWN")
			elif "MAINT" in str(vips[i]):
				currentstat.append("MAINT")
			else:
				currentstat.append("none")

			if firstrun == False:
				if (currentstat[i] != oldstat[i] and currentstat[i]!="none") and ("FRONTEND" not in str(vips[i]) and "BACKEND" not in str(vips[i])):
					servername = str(vips[i])
					servername = servername.split(",")
					realserver = servername[0]
					server = servername[1]
					alert = "Backend: "+realserver[2:]+", server: "+server+"  has changed status and is now "+ currentstat[i] + " at " + serv 
					funct.telegram_send_mess(str(alert), ip=serv)
					funct.logging("localhost", " "+alert, alerting=1)
		firstrun = False
		oldstat = []
		oldstat = currentstat
		currentstat = []
		time.sleep(60)	
				
		if killer.kill_now:
			break
	
	funct.logging("localhost", " Worker shutdown for: "+serv, alerting=1)
Example #2
0
def main(serv, port):
    port = str(port)
    firstrun = True
    currentstat = []
    oldstat = []
    readstats = ""
    killer = GracefulKiller()
    old_stat_service = ""

    while True:
        try:
            readstats = subprocess.check_output(
                ["echo show stat | nc " + serv + " " + port], shell=True)
        except CalledProcessError as e:
            if firstrun == False:
                cur_stat_service = "error"
                if old_stat_service != cur_stat_service:
                    alert = "Can't connect to HAProxy service at " + serv
                    funct.telegram_send_mess(str(alert), ip=serv)
                    funct.logging("localhost", " " + alert, alerting=1)

            firstrun = False
            old_stat_service = cur_stat_service
            time.sleep(60)
            continue
        except OSError as e:
            print(e)
            sys.exit()
        else:
            cur_stat_service = "Ok"
            if firstrun == False:
                if old_stat_service != cur_stat_service:
                    alert = "Now UP HAProxy service at " + serv
                    funct.telegram_send_mess(str(alert), ip=serv)
                    funct.logging("localhost", " " + alert, alerting=1)
                    firstrun = True
                    time.sleep(5)
            old_stat_service = cur_stat_service

        vips = readstats.splitlines()

        for i in range(0, len(vips)):
            if "UP" in str(vips[i]):
                currentstat.append("UP")
            elif "DOWN" in str(vips[i]):
                currentstat.append("DOWN")
            elif "MAINT" in str(vips[i]):
                currentstat.append("MAINT")
            else:
                currentstat.append("none")

            if firstrun == False:
                if (currentstat[i] != oldstat[i] and
                        currentstat[i] != "none") and ("FRONTEND" not in str(
                            vips[i]) and "BACKEND" not in str(vips[i])):
                    servername = str(vips[i])
                    servername = servername.split(",")
                    realserver = servername[0]
                    server = servername[1]
                    alert = "Backend: " + realserver[
                        2:] + ", server: " + server + "  has changed status to " + currentstat[
                            i] + " at " + serv
                    funct.telegram_send_mess(str(alert), ip=serv)
                    funct.logging("localhost", " " + alert, alerting=1)
        firstrun = False
        oldstat = []
        oldstat = currentstat
        currentstat = []
        time.sleep(60)

        if killer.kill_now:
            break

    funct.logging("localhost", " Worker shutdown for: " + serv, alerting=1)