Guest User

Switchboard code

a guest
Nov 14th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.30 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import RPi.GPIO as GPIO
  4. from time import sleep
  5. import requests
  6. import time
  7. import json
  8. import re
  9.  
  10.  
  11. # Create a function to cycle through all the LEDs one by one.
  12. def ledcycle():
  13.     # Define which pins have LEDs attached to them.
  14.     leds = [7, 11, 12, 13, 15, 16, 18, 22, 29, 31, 32, 31, 29, 22, 18, 16, 15, 13, 12, 11, 7]
  15.     # Disable "GPIO pin in use" warning.
  16.     GPIO.setwarnings(False)
  17.     # Pins are being referenced by the number of the pin, and not the "Broadcom SOC channel" number (GPIO.BCM).
  18.     GPIO.setmode(GPIO.BOARD)
  19.     # Set the selected pins as output.
  20.     GPIO.setup(leds, GPIO.OUT)
  21.     # Cycle through each LED and turn it on and off again.
  22.     for led in leds:
  23.     GPIO.output(led, GPIO.HIGH)
  24.     time.sleep(0.1)
  25.     GPIO.output(led, GPIO.LOW)
  26.  
  27. # Create a function to blink all the LEDs three times.
  28. def ledblink():
  29.     # Define which pins have LEDs attached to them.
  30.     leds = [7, 11, 12, 13, 15, 16, 18, 22, 29, 31, 32]
  31.     # Disable "GPIO pin in use" warning.
  32.     GPIO.setwarnings(False)
  33.     # Pins are being referenced by the number of the pin.
  34.     GPIO.setmode(GPIO.BOARD)
  35.     # Set the selected pins as output.
  36.     GPIO.setup(leds, GPIO.OUT)
  37.     # Turn all LEDs on and off three times.
  38.     for x in range(3):
  39.         GPIO.output(leds, GPIO.HIGH)
  40.         sleep(0.2)
  41.         GPIO.output(leds, GPIO.LOW)
  42.         sleep(0.2)
  43.  
  44. # Check the status and uptime for each device in LibreNMS.
  45. def checks():
  46.     # Define which LED is attached to which GPIO pin.
  47.     LED_1_drift = 7
  48.     LED_2_fejl = 11
  49.     LED_3_alarm = 12
  50.     LED_4_fejl = 13
  51.     LED_5_alarm = 15
  52.     LED_6_fejl = 16
  53.     LED_7_alarm = 18
  54.     LED_8_fejl = 22
  55.     LED_9_alarm = 29
  56.     LED_10_fejl = 31
  57.     LED_11_alarm = 32
  58.  
  59.     # Disable "GPIO pin in use" warning.
  60.     GPIO.setwarnings(False)
  61.     # Pins are being referenced by the number of the pin.
  62.     GPIO.setmode(GPIO.BOARD)
  63.  
  64.     # Set the initial GPIO to low (off) for all LEDs.
  65.     led_alarmer = [LED_1_drift, LED_2_fejl, LED_3_alarm, LED_4_fejl, LED_5_alarm, LED_6_fejl, LED_7_alarm, LED_8_fejl, LED_9_alarm, LED_10_fejl, LED_11_alarm]
  66.     GPIO.setup(led_alarmer, GPIO.OUT, initial=GPIO.LOW)
  67.  
  68.     # Import all active (not ignored and not disabled) devices from LibreNMS.
  69.     headers = {
  70.         'X-Auth-Token': 'hmsudc8rnwr1phx9dwvrbmybc0lwsgsy',
  71.     }
  72.     librenms_poll = requests.get('https://librenms.org/api/v0/devices?type=active', headers = headers)
  73.  
  74.     # Create an array for each location to be filled with devices.
  75.     ONE_devices = []
  76.     TWO_devices = []
  77.     THREE_devices = []
  78.     FOUR_devices = []
  79.     FIVE_devices = []
  80.  
  81.     devices = librenms_poll.json()
  82.  
  83.     # Cycle through all devices and sort them by their hostname.
  84.     for item in devices['devices']:
  85.     if re.match('.*-ONE-.*', item['hostname']):
  86.         ONE_devices.append(item)
  87.     elif re.match('.*-TWO-.*', item['hostname']):
  88.             TWO_devices.append(item)
  89.     elif re.match('.*-THREE-.*', item['hostname']):
  90.             THREE_devices.append(item)
  91.     elif re.match('.*-FOUR.*', item['hostname']):
  92.             THREE_devices.append(item)
  93.     else:
  94.         FIVE_devices.append(item)
  95.  
  96.  
  97.     # ONE
  98.     LED_3_alarm_bit = 0
  99.     # Cycle through each device in the ONE_devices array and check if their status = 0.
  100.     for item in ONE_devices:
  101.     # If the status of even one device is 0, the value of the LED_3_alarm_bit is changed to 1.
  102.     if item['status'] == 0:
  103.         LED_3_alarm_bit = 1
  104.  
  105.     if LED_3_alarm_bit == 1:
  106.     # If a device is down, change the corresponding LED to on.
  107.     GPIO.output(LED_3_alarm, GPIO.HIGH)
  108.     # Disable the "LED_1_drift" LED.
  109.     GPIO.output(LED_1_drift, GPIO.LOW)
  110.     else:
  111.     # If no devices are down, turn off the LED_3_alarm LED.
  112.     GPIO.output(LED_3_alarm, GPIO.LOW)
  113.  
  114.     LED_2_fejl_bit = 0
  115.     # Cycle through each device in the ONE_devices array and check if any of them has an uptime of less than 24 hours.
  116.     for item in ONE_devices:
  117.     # If a device has an uptime of less than 24 hours, change the value of the LED_2_fejl_bit to 1.
  118.     if item['uptime'] < 86400:
  119.         LED_2_fejl_bit = 1
  120.  
  121.     if LED_2_fejl_bit == 1:
  122.     # If a device has been restarted within the last 24 hours, change the corresponding LED to on.
  123.     GPIO.output(LED_2_fejl, GPIO.HIGH)
  124.     # Disable the "LED_1_drift" LED.
  125.     GPIO.output(LED_1_drift, GPIO.LOW)
  126.     else:
  127.     # If no devices have been restarted, turn off the LED_2_fejl LED.
  128.     GPIO.output(LED_2_fejl, GPIO.LOW)
  129.    
  130.    
  131.     # TWO
  132.     LED_5_alarm_bit = 0
  133.     for item in TWO_devices:
  134.         if item['status'] == 0:
  135.             LED_5_alarm_bit = 1
  136.  
  137.     if LED_5_alarm_bit == 1:
  138.         GPIO.output(LED_5_alarm, GPIO.HIGH)
  139.         GPIO.output(LED_1_drift, GPIO.LOW)
  140.     else:
  141.         GPIO.output(LED_5_alarm, GPIO.LOW)
  142.  
  143.     LED_4_fejl_bit = 0
  144.     for item in TWO_devices:
  145.         if item['uptime'] < 86400:
  146.             LED_4_fejl_bit = 1
  147.  
  148.     if LED_4_fejl_bit == 1:
  149.         GPIO.output(LED_4_fejl, GPIO.HIGH)
  150.         GPIO.output(LED_1_drift, GPIO.LOW)
  151.     else:
  152.         GPIO.output(LED_4_fejl, GPIO.LOW)
  153.    
  154.    
  155.     # THREE
  156.         LED_7_alarm_bit = 0
  157.     for item in THREE_devices:
  158.         if item['status'] == 0:
  159.             LED_7_alarm_bit = 1
  160.  
  161.     if LED_7_alarm_bit == 1:
  162.         GPIO.output(LED_7_alarm, GPIO.HIGH)
  163.         GPIO.output(LED_1_drift, GPIO.LOW)
  164.     else:
  165.         GPIO.output(LED_7_alarm, GPIO.LOW)
  166.  
  167.     LED_6_fejl_bit = 0
  168.     for item in THREE_devices:
  169.         if item['uptime'] < 86400:
  170.             LED_6_fejl_bit = 1
  171.  
  172.     if LED_6_fejl_bit == 1:
  173.         GPIO.output(LED_6_fejl, GPIO.HIGH)
  174.         GPIO.output(LED_1_drift, GPIO.LOW)
  175.     else:
  176.         GPIO.output(LED_6_fejl, GPIO.LOW)
  177.    
  178.    
  179.     # FOUR
  180.     LED_9_alarm_bit = 0
  181.     for item in FOUR_devices:
  182.         if item['status'] == 0:
  183.             LED_9_alarm_bit = 1
  184.  
  185.     if LED_9_alarm_bit == 1:
  186.         GPIO.output(LED_9_alarm, GPIO.HIGH)
  187.         GPIO.output(LED_1_drift, GPIO.LOW)
  188.     else:
  189.         GPIO.output(LED_9_alarm, GPIO.LOW)
  190.  
  191.     LED_8_fejl_bit = 0
  192.     for item in FOUR_devices:
  193.         if item['uptime'] < 86400:
  194.             LED_8_fejl_bit = 1
  195.  
  196.     if LED_8_fejl_bit == 1:
  197.         GPIO.output(LED_8_fejl, GPIO.HIGH)
  198.         GPIO.output(LED_1_drift, GPIO.LOW)
  199.     else:
  200.         GPIO.output(LED_8_fejl, GPIO.LOW)
  201.    
  202.    
  203.     # FIVE
  204.     LED_11_alarm_bit = 0
  205.     for item in FIVE_devices:
  206.         if item['status'] == 0:
  207.             LED_11_alarm_bit = 1
  208.  
  209.     if LED_11_alarm_bit == 1:
  210.         GPIO.output(LED_11_alarm, GPIO.HIGH)
  211.         GPIO.output(LED_1_drift, GPIO.LOW)
  212.     else:
  213.         GPIO.output(LED_11_alarm, GPIO.LOW)
  214.  
  215.     LED_10_fejl_bit = 0
  216.     for item in FIVE_devices:
  217.         if item['uptime'] < 86400:
  218.             LED_10_fejl_bit = 1
  219.  
  220.     if LED_10_fejl_bit == 1:
  221.         GPIO.output(LED_10_fejl, GPIO.HIGH)
  222.         GPIO.output(LED_1_drift, GPIO.LOW)
  223.     else:
  224.         GPIO.output(LED_10_fejl, GPIO.LOW)
  225.  
  226.     # DRIFT
  227.     # If no errors have been detected, turn on the "LED_1_drift" LED.
  228.     if LED_2_fejl_bit + LED_3_alarm_bit + LED_4_fejl_bit + LED_5_alarm_bit + LED_6_fejl_bit + LED_7_alarm_bit + LED_8_fejl_bit + LED_9_alarm_bit + LED_10_fejl_bit + LED_11_alarm_bit == 0:
  229.         GPIO.output(LED_1_drift, GPIO.HIGH)
  230.  
  231.  
  232. # Run the ledblink() and ledcycle() functions only once every time the Raspberry Pi is rebooted.        
  233. i = 0
  234. while True:
  235.     if i == 0:
  236.         ledblink()
  237.         ledcycle()
  238.         i += 1
  239.     if i == 1:
  240.         checks()
  241.         sleep(60)
  242.  
  243. GPIO.cleanup
Add Comment
Please, Sign In to add comment