#!/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 ANY_VALUE(p.network_id), COUNT(*) networks 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' GROUP BY b.dhcp_agent_id DESC"
cur.execute(sql)

header_printed = 0;
for row in cur.fetchall():
  if header_printed == 0:
    print(" in this networks dhcpagents ports are down:")
    header_printed = 1
  print("network id " + row[0])
  err = 1 # Warning
if header_printed == 1:
  print

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

sys.exit(err);

