#!/usr/bin/env python
# Author: Dainius Jocas

# This script selects a monitor for an agent.
# if one of RPis was shut down system will optimize itself. 

# input: list of possible monitors (file: monitors.txt)
# output: invoke client_2_jocas.py script with an IP of a selected monitor 


import os
import sys

# read file with list of IP's
f = open('monitors.txt')
monitors = f.readlines()
f.close()
# loop in list of IP's
for monitor in monitors:
    monitor = monitor.strip()
    query = "snmpwalk -m DJ-MIB -v 2c -c public " + monitor + " piCPULoad";
    load = os.popen(query).read().strip().split("\"")[1]
    print(load)
    # Check if load is <80%
    if (float(load) < 80):
        # yes: assign monitor and call client_2_jocas.py script
        call = "python client_2_jocas.py " +  monitor + " 34567 1"
        print(call)
        os.system(call)
    else:
        print("continue looping")    
        
# The are no monitors
