#!/usr/bin/env python2
#
# The MySQLdb package only works in python2, so far

import MySQLdb     # pSQL setzt auf MySQLdb auf
import os
import sys

db_host = '127.0.0.1'
db_user = 'noris_monitoring'
db_pass = os.environ['DBAUTH']

err = 0
ports = {}
instances = {}

neutron_db = MySQLdb.connect(host=db_host, user=db_user, passwd=db_pass, db='neutron')

cur = neutron_db.cursor()

sql = "select p.network_id, a.host, b.dhcp_agent_id from neutron.ports p JOIN neutron.networkdhcpagentbindings b ON p.network_id = b.network_id JOIN  neutron.agents a ON a.id = b.dhcp_agent_id  WHERE device_owner = 'network:dhcp' and status ='DOWN' and device_id !='reserved_dhcp_port'"
cur.execute(sql)

header_printed = 0;
for row in cur.fetchall():
  if header_printed == 0:
    print("The following dhcpagents ports are down:")
    header_printed = 1
  print("network ID " + row[0] + ", host " + row[1] + ", dhcpagent id" + row[2])
  err = 1 # Warning
if header_printed == 1:
  print

if err == 0:
  print("OK")

sys.exit(err);
