Created
May 10, 2017 14:27
-
-
Save ericpulvino/c09d1b6a56eb8e7d1272258e126cb8da to your computer and use it in GitHub Desktop.
Quick Script to Send New Parameters to Syslog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import time | |
import json | |
import subprocess | |
import logging | |
import logging.handlers | |
# Log to CLI and Syslog or just CLI | |
log_to_syslog=True | |
# Frequency of Resource Utilization Checks (in seconds) | |
check_interval=30 | |
# Configurable Thresholds (in Percent) | |
route_utilization_threshold=90 | |
acl_utilization_threshold=90 | |
# Logging Setup | |
if log_to_syslog: | |
log = logging.getLogger(__name__) | |
log.setLevel(logging.DEBUG) | |
handler = logging.handlers.SysLogHandler(address = '/dev/log') | |
formatter = logging.Formatter('custom_monitoring: %(message)s') | |
handler.setFormatter(formatter) | |
log.addHandler(handler) | |
def print_and_log(somestring): | |
print(somestring) | |
if log_to_syslog: log.info(somestring) | |
def main(): | |
print(""" | |
################################################ | |
# # | |
# Custom Syslog Monitoring # | |
# originally written by Eric Pulvino # | |
# # | |
################################################ | |
""") | |
print_and_log(" STARTING UP.") | |
print("Route Utilization Threshold is set to log if greater than: %s Percent"%route_utilization_threshold) | |
print("ACL Utilization Threshold is set to log if greater than: %s Percent"%acl_utilization_threshold) | |
while True: | |
try: | |
raw_output=subprocess.check_output(["/usr/cumulus/bin/cl-resource-query -j"],shell=True) | |
except: | |
print_and_log("ERROR: Unable to collect output from cl-resource-query!") | |
exit(1) | |
try: | |
cl_resource_query=json.loads(raw_output) | |
except: | |
print_and_log("WARNING: could not parse cl-resource-query output as JSON.") | |
continue | |
IPv4_route_capacity=cl_resource_query[u'route_0_entry'] | |
IPv6_route_capacity=cl_resource_query[u'route_1_entry'] | |
ingress_ACL_capacity=cl_resource_query[u'in_acl_entry'] | |
egress_ACL_capacity=cl_resource_query[u'eg_acl_entry'] | |
# Route Capacity Checks | |
if IPv4_route_capacity[u'percentage'] >= route_utilization_threshold: | |
print_and_log("IPv4 Route Utilization: %s (%s%%) of %s routes"%(IPv4_route_capacity[u'count'],IPv4_route_capacity[u'percentage'],IPv4_route_capacity[u'max'])) | |
if IPv6_route_capacity[u'percentage'] >= route_utilization_threshold: | |
print_and_log("IPv6 Route Utilization: %s (%s%%) of %s routes"%(IPv6_route_capacity[u'count'],IPv6_route_capacity[u'percentage'],IPv6_route_capacity[u'max'])) | |
# ACL Capacity Checks | |
if ingress_ACL_capacity[u'percentage'] >= acl_utilization_threshold: | |
print_and_log("Ingress ACL Entry Utilization: %s (%s%%) of %s entries"%(ingress_ACL_capacity[u'count'],ingress_ACL_capacity[u'percentage'],ingress_ACL_capacity[u'max'])) | |
if egress_ACL_capacity[u'percentage'] >= acl_utilization_threshold: | |
print_and_log("Egress ACL Entry Utilization: %s (%s%%) of %s entries"%(egress_ACL_capacity[u'count'],egress_ACL_capacity[u'percentage'],egress_ACL_capacity[u'max'])) | |
time.sleep(check_interval) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment