Using Python to Monitor Alarms on Network Equipment with PuTTY

Monitoring alarms on network equipment is a critical task for ensuring the stability and security of IT infrastructure. By integrating Python with tools like PuTTY, administrators can automate the monitoring process, reducing the risk of missing crucial alerts. This article explores how Python can be utilized to monitor a network server for alarms, providing a real-world example of a script that interacts with PuTTY to achieve this goal.

Overview of the Setup

In this scenario, the network server generates logs containing various alarms and notifications. These logs are accessible via SSH, a secure protocol that allows administrators to remotely connect to the server. PuTTY, a popular SSH client, is commonly used to establish these connections. By leveraging Python, we can automate the process of connecting to the server, reading the logs, and parsing them for specific alarms or alerts.

Step 1: Setting Up the Python Environment

Before diving into the code, ensure that Python is installed on your system. Additionally, you will need the paramiko library, which allows Python to interact with SSH servers programmatically. Install paramiko using pip:

pip install paramiko

Step 2: Writing the Python Script

The Python script will automate the process of connecting to the server via SSH, executing commands to retrieve logs, and parsing those logs for specific alarms. The script can be structured as follows:

  1. Import Required Libraries:
import paramiko
import time
import re

2. Establish the SSH Connection:

def create_ssh_client(server, port, user, password):
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(server, port, user, password)
    return client
  • Here, create_ssh_client establishes a secure connection to the server. Replace serverportuser, and password with the relevant details for your network server.

3. Execute Commands to Retrieve Logs:

def fetch_logs(ssh_client):
    stdin, stdout, stderr = ssh_client.exec_command('cat /var/log/syslog')
    return stdout.readlines()
  • This function sends the command cat /var/log/syslog to the server, which retrieves the content of the system log file. Adjust the log file path based on your server configuration.

4. Parse Logs for Alarms:

def parse_logs_for_alarms(log_lines):
    alarms = []
    for line in log_lines:
        if re.search(r'(ALARM|ERROR|CRITICAL)', line):
            alarms.append(line)
    return alarms
  • The parse_logs_for_alarms function searches the logs for specific keywords such as ALARMERROR, or CRITICAL. These keywords indicate potential issues that need attention.

5. Send Alerts:

def send_alerts(alarms):
    for alarm in alarms:
        print(f"ALERT: {alarm}")
  • The send_alerts function could be expanded to send emails, SMS messages, or push notifications to administrators, depending on the severity of the alarm. For simplicity, this example prints the alarms to the console.

6. Main Function to Tie It All Together:

if __name__ == '__main__':
    ssh_client = create_ssh_client('192.168.1.100', 22, 'admin', 'password')
    while True:
        logs = fetch_logs(ssh_client)
        alarms = parse_logs_for_alarms(logs)
        if alarms:
            send_alerts(alarms)
        time.sleep(60)  # Wait for 60 seconds before checking again

This main function establishes the SSH connection, retrieves logs, parses them for alarms, and sends alerts every 60 seconds. The server IP, port, user credentials, and log file path should be adjusted to fit your specific environment.

Step 3: Running the Script

Save the script as monitor_alarms.py. To execute the script, simply run:

python monitor_alarms.py

This script will continuously monitor the server logs, alerting you whenever it detects an alarm. You can customize the frequency of checks and the method of alerts to suit your needs.

Enhancements and Considerations

  • Error Handling: Implement robust error handling to manage network issues, incorrect credentials, or server downtime.
  • Multiple Servers: Expand the script to monitor multiple servers simultaneously by creating threads or using asynchronous programming techniques.
  • Log Rotation: Consider how log rotation on the server might affect your script, and ensure it can handle changes in log file names or locations.

By integrating Python with PuTTY and SSH, you can create a powerful, automated alarm monitoring system that enhances the reliability of your network infrastructure. This approach reduces the need for manual log inspection and ensures that critical alarms are addressed promptly.


Rea-Life Example

Let’s assume you’re tasked with monitoring the temperature alarm on a Cisco Catalyst 3850 using Python and PuTTY (via SSH). Below is a quick example script that connects to the Catalyst 3850, retrieves the temperature status, and checks for any alarms.

Step 1: Install Required Libraries

If you haven’t already, install the paramiko library for SSH communication:

pip install paramiko

Step 2: Python Script to Check Temperature Alarms

Here’s a script that connects to the Catalyst 3850 and checks for temperature alarms:

import paramiko
import time

def create_ssh_client(server, port, user, password):
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(server, port, user, password)
    return client

def check_temperature_alarm(ssh_client):
    stdin, stdout, stderr = ssh_client.exec_command('show environment temperature status')
    output = stdout.readlines()
    return output

def parse_temperature_output(output):
    alarms = []
    for line in output:
        if "TEMPERATURE" in line and ("Warning" in line or "Critical" in line):
            alarms.append(line.strip())
    return alarms

def send_alerts(alarms):
    for alarm in alarms:
        print(f"TEMPERATURE ALERT: {alarm}")

if __name__ == '__main__':
    ssh_client = create_ssh_client('192.168.1.100', 22, 'admin', 'password')
    while True:
        output = check_temperature_alarm(ssh_client)
        alarms = parse_temperature_output(output)
        if alarms:
            send_alerts(alarms)
        time.sleep(60)  # Check every 60 seconds

Explanation

  1. SSH Connection Setup:
  • The create_ssh_client function establishes an SSH connection to the Cisco Catalyst 3850 switch using the provided IP address, port, username, and password.

2. Check Temperature Status:

  • The check_temperature_alarm function executes the show environment temperature status command on the switch, which returns the temperature status of the switch’s components.

3. Parse Output for Alarms:

  • The parse_temperature_output function scans the output for lines containing the word “TEMPERATURE” and the keywords “Warning” or “Critical.” These keywords indicate potential temperature alarms.

4. Alert Mechanism:

  • The send_alerts function prints the detected alarms to the console. This can be expanded to send notifications via email, SMS, etc.

5. Main Loop:

  • The script continuously checks the temperature status every 60 seconds, making it suitable for real-time monitoring.

Running the Script

To run this script, save it as monitor_temperature.py and execute it with Python:

python monitor_temperature.py

Customization

  • IP Address and Credentials: Replace '192.168.1.100'22'admin', and 'password' with the actual IP address, SSH port, username, and password of your Catalyst 3850.
  • Alerts: Customize the send_alerts function to integrate with your preferred alerting system.

This script provides a basic but functional method for monitoring the temperature status on a Cisco Catalyst 3850 switch, allowing for real-time alerts in case of potential overheating issues.


Thank you for following along with this tutorial. We hope you found it helpful and informative. If you have any questions, or if you would like to suggest new Python code examples or topics for future tutorials/articles, please feel free to join and comment. Your feedback and suggestions are always welcome!

You can find the same tutorial on Medium.com.

Leave a Reply