Esempio n. 1
0
def getJobForHost(src_hostname, dt):
    jobs = []
    conn = MongoClient(config.MONGO_SERVER)[config.MONGO_DB]
    ping_dt = dt - config.PING_INTERVAL
    iperf_dt = dt - config.IPERF_INTERVAL
    iperf_hard_dt = dt - config.IPERF_HARD_INTERVAL
    src_host = common.select_host(src_hostname)

    query = {'src': src_hostname, 'last_iperf_dt': {"$lt": iperf_dt}}
    src_group = common.select_group(src_host["gid"])
    if config.ENABLE_HOSTGROUP:
        if src_group["status"] == 'external':
            members = common.group_members(src_group)
            logging.error("internal members :%s" % str(members))
            query['dest'] = {"$in": members}

    running_hosts = conn.host.find({'status': 'running'}).count()
    if running_hosts >= config.MAX_RUNNING:
        query['src'] = 'this should found nothing'

    flows = conn.flow.find(query).sort('last_iperf_dt', 1)
    logging.error("flows count: %d " % flows.count())
    # Do only one iperf to made the whole system work concurrently
    for flow in flows:
        dest_host = common.select_host(flow['dest'])
        dest_group = common.select_group(dest_host["gid"])
        # Soft Deadline
        if dest_host['status'] == 'idle':
            logging.error("soft:" + src_host["hostname"] + "->" +
                          dest_host["hostname"])
            jobs.append(common.createIperfJob(src_host, dest_host))
            common.update_group_status(src_group, dest_group)
            break
        # Hard Deadline
        if dest_host[
                'status'] == 'busy' and flow['last_iperf_dt'] < iperf_hard_dt:
            logging.error("hard:" + src_host["hostname"] + "->" +
                          dest_host["hostname"])
            jobs.append(common.createIperfJob(src_host, dest_host))
            common.update_group_status(src_group, dest_group)
            break

    query = conn['flow'].find({
        'src': src_hostname,
        'last_ping_dt': {
            "$lt": ping_dt
        }
    })
    flows = query.sort('last_ping_dt', 1)
    # Ping does not toggle host status to running, but need to be done when idle
    # Src host running is okay since it will complete iperf first btw
    for flow in flows:
        dest_host = common.select_host(flow['dest'])
        if dest_host['status'] == 'idle':
            jobs.append({'type': 'ping', 'hostname': dest_host["hostname"]})

    return jobs
Esempio n. 2
0
def getJobForHost(src_hostname, dt):
  jobs = []
  conn = MongoClient(config.MONGO_SERVER)[config.MONGO_DB]
  ping_dt = dt - config.PING_INTERVAL
  iperf_dt = dt - config.IPERF_INTERVAL
  iperf_hard_dt = dt - config.IPERF_HARD_INTERVAL
  src_host = common.select_host(src_hostname)


  query = {
    'src' : src_hostname,
    'last_iperf_dt': {"$lt" : iperf_dt}
  }
  src_group = common.select_group(src_host["gid"])
  if config.ENABLE_HOSTGROUP:
    if src_group["status"] == 'external':
      members = common.group_members(src_group)
      logging.error("internal members :%s" %str(members))
      query['dest'] = { "$in": members}

  running_hosts = conn.host.find({'status':'running'}).count()
  if running_hosts >= config.MAX_RUNNING:
    query['src'] = 'this should found nothing'

  flows = conn.flow.find(query).sort('last_iperf_dt',1)
  logging.error("flows count: %d " %flows.count())
  # Do only one iperf to made the whole system work concurrently
  for flow in flows:
    dest_host = common.select_host(flow['dest'])
    dest_group = common.select_group(dest_host["gid"])
    # Soft Deadline
    if dest_host['status'] == 'idle':
      logging.error("soft:" + src_host["hostname"] + "->" + dest_host["hostname"])
      jobs.append(common.createIperfJob(src_host, dest_host))
      common.update_group_status(src_group, dest_group)
      break;
    # Hard Deadline
    if dest_host['status'] == 'busy' and flow['last_iperf_dt'] < iperf_hard_dt:
      logging.error("hard:" + src_host["hostname"] + "->" + dest_host["hostname"])
      jobs.append(common.createIperfJob(src_host, dest_host))
      common.update_group_status(src_group, dest_group)
      break;

  query = conn['flow'].find({
    'src' : src_hostname,
    'last_ping_dt': {"$lt" : ping_dt}
  })
  flows = query.sort('last_ping_dt',1)
  # Ping does not toggle host status to running, but need to be done when idle
  # Src host running is okay since it will complete iperf first btw
  for flow in flows:
    dest_host = common.select_host(flow['dest'])
    if dest_host['status'] == 'idle':
      jobs.append({'type':'ping','hostname': dest_host["hostname"]})

  return jobs
Esempio n. 3
0
def main():
    parser = argparse.ArgumentParser(
        description="Upgrade utility for Mikrotik routers")
    parser.add_argument("hosts",
                        metavar="host_name",
                        nargs="*",
                        default="",
                        help="Router name or IP address")
    options = parser.parse_args()

    credentials = json.loads(
        base64.b64decode(
            os.environ["AO_MIKROTIK_CREDENTIALS"]).decode("utf-16"))
    if not credentials["username"]:
        sys.exit("ERROR: Empty user name has been provided")

    config_file_name = common.get_config_file_path()
    if os.path.isfile(config_file_name):
        with open(config_file_name) as conf_f:
            config = json.load(conf_f)
    else:
        config = {"recent_hosts": []}

    if not options.hosts:
        # [!] Saving user selection as list
        options.hosts = [common.select_host(config["recent_hosts"])]

    for host in options.hosts:
        print(colorama.Fore.CYAN + colorama.Style.BRIGHT +
              f"=== Upgrading RouterOS and firmware on '{host}' ===" +
              colorama.Style.RESET_ALL)

        # Saving host before connecting to save some typing in case something
        # went wrong
        try:
            config["recent_hosts"].pop(config["recent_hosts"].index(host))
        except ValueError:
            pass
        config["recent_hosts"].insert(0, host)
        with open(config_file_name, "w", encoding="utf-8") as conf_f:
            json.dump(config, conf_f, ensure_ascii=False, indent=4)

        ssh_client = common.ssh_connect(host, credentials)

        upgrade_ros(host, ssh_client, credentials)

        upgrade_firmware(host, ssh_client, credentials)

        ssh_client.close()

        print(colorama.Fore.CYAN + colorama.Style.BRIGHT + f"{host}: done" +
              colorama.Style.RESET_ALL)
Esempio n. 4
0
def record_values(src_hostname, status, values, dt):
  conn = MongoClient(config.MONGO_SERVER)[config.MONGO_DB]
  logging.error(src_hostname +" - values : " +str(values))
  src_host = common.select_host(src_hostname)
  src_group = common.select_group(src_host["gid"])
  for r in values:
    row = dict()
    row["src"] = src_hostname
    row["dt"] = dt
    row["type"] = r["type"]
    row["dest"] = r["dest"]
    if row["type"] == 'utilization':
      row["cpu"] = float(r["cpu"])
      row["net"] = float(r["net"])
      row["loadavg"] = float(r["loadavg"])
    else:
      flow_query = {'src': src_hostname, 'dest': r["dest"]}
      flow = conn.flow.find_one(flow_query)
      if row["type"] == 'ping':
        row["min"] = float(r["min"])
        row["max"] = float(r["max"])
        row["avg"] = float(r["avg"])
        flow["last_ping_dt"] = dt
      elif row["type"] == 'iperf':
        row["bandwidth"] = float(r["bandwidth"])
        flow["last_iperf_dt"] = dt
      else:
        logging.error("invalid value type")
        return False
      conn.flow.update({'_id': flow["_id"]}, flow)

    conn.values.insert(row)
    logging.error("recording : %s" %str(row['src']))

  if status == 'idle':
    src_host["status"] = 'idle'
    src_group["status"] = 'idle'
    conn.hostgroup.update({'_id':src_group["_id"]}, src_group)
  else: 
    src_host["status"] = status
  conn.host.update({'_id':src_host["_id"]}, src_host)
Esempio n. 5
0
    for flow in flows:
        dest_host = common.select_host(flow['dest'])
        if dest_host['status'] == 'idle':
            jobs.append({'type': 'ping', 'hostname': dest_host["hostname"]})

    return jobs


if __name__ == '__main__':
    conn = MongoClient(config.MONGO_SERVER)[config.MONGO_DB]
    #  iperf_dt = datetime.datetime.now() - IPERF_INTERVAL
    dt = datetime.datetime(2013, 1, 1, 0, 0, 0)
    jobs = getJobForHost('fe', dt)
    print jobs
    # tester: running=2
    running_count = conn['host'].find({'status': 'running'}).count()
    if running_count != 2:
        print "error running != 2 , =%d" % running_count
    host_fe = common.select_host('fe')
    host_c0 = common.select_host('c0')
    host_c1 = common.select_host('c1')
    host_c2 = common.select_host('c2')
    host_c3 = common.select_host('c3')
    host_c4 = common.select_host('c4')
    host_c5 = common.select_host('c5')
    host_c6 = common.select_host('c6')
    print common.hosts_same_group(host_fe, host_c0)
    print common.hosts_same_group(host_c1, host_c0)
    print common.hosts_same_group(host_c2, host_c3)
    print common.hosts_same_group(host_c4, host_c6)
Esempio n. 6
0
  # Src host running is okay since it will complete iperf first btw
  for flow in flows:
    dest_host = common.select_host(flow['dest'])
    if dest_host['status'] == 'idle':
      jobs.append({'type':'ping','hostname': dest_host["hostname"]})

  return jobs

if __name__ == '__main__':
  conn = MongoClient(config.MONGO_SERVER)[config.MONGO_DB]
#  iperf_dt = datetime.datetime.now() - IPERF_INTERVAL
  dt = datetime.datetime(2013, 1,1, 0,0,0)
  jobs = getJobForHost('fe', dt)
  print jobs
  # tester: running=2 
  running_count = conn['host'].find({'status': 'running'}).count()
  if running_count != 2:
    print "error running != 2 , =%d" %running_count
  host_fe = common.select_host('fe')
  host_c0 = common.select_host('c0')
  host_c1 = common.select_host('c1')
  host_c2 = common.select_host('c2')
  host_c3 = common.select_host('c3')
  host_c4 = common.select_host('c4')
  host_c5 = common.select_host('c5')
  host_c6 = common.select_host('c6')
  print common.hosts_same_group(host_fe, host_c0)
  print common.hosts_same_group(host_c1, host_c0)
  print common.hosts_same_group(host_c2, host_c3)
  print common.hosts_same_group(host_c4, host_c6)