Giter Site home page Giter Site logo

puppet-finland / net-snmp-systemd-service-status Goto Github PK

View Code? Open in Web Editor NEW
4.0 6.0 1.0 16 KB

A Net-SNMP "pass" extension script that generates "is service up" data for all detected systemd services

License: BSD 2-Clause "Simplified" License

Python 100.00%

net-snmp-systemd-service-status's Introduction

net-snmp-systemd-service-status

This is a snmpd (Net-SNMP) "pass_persist" extension script that generates "is service up" data for all detected systemd services. On a high level it works as follows:

  1. Uses a private OID subtree ("1.3.9950.1") as a prefix for all OIDs
  2. Lists systemd service unit files on the system
  3. Filters out services that can have multiple instances ("@.service")
  4. Gets the numeric return value of systemctl is-active for each service
  5. Convert the name of the service into a valid OID by changing every character into its equivalent decimal ASCII code
  6. Store all data (oid, data type, value, service name) in a dictionary
  7. Create a sorted list of oids used for snmpwalk / getnext
  8. Provide snmpd the data it requests by listening on standard input and answering queries as documented in snmpd.conf

This script can be used interactively as well. No parameters are needed, just talk to the script like described in snmpd.conf.

Configuration

Clone the repository, then add a line such as this to snmpd.conf:

pass_persist .1.3.9950.1 /etc/snmp/net-snmp-systemd-service-status/systemd-service-status.py

After restarting snmpd you should be able to query or walk through systemd service status with

snmpwalk -On -v 3 -u user -l authPriv -a sha -A pass -x AES -X pass localhost .1.3.9950.1

or

snmpwalk -On -v2c -c public localhost .1.3.9950.1

Performance

This script has seen multiple iterations which differ a great deal performance-vise. All tests were conducted on a fairly low power Ubuntu 18.04 VM:

  • Original "pass" script was launched once per OID by snmpd. Querying all units (180) took about 10 seconds.
  • First iteration of "pass_persist" script ran "systemctl is-active" for detected (180) units. Querying all units took about 1.6 seconds.
  • Second (current) iteration of "pass_persist" script that gets all the data using one systemctl command. Querying all (61) services took about 0.15 seconds.

net-snmp-systemd-service-status's People

Contributors

mattock avatar

Stargazers

Jordan Williams avatar Aaron avatar  avatar Ilija Vukotic avatar

Watchers

 avatar James Cloos avatar Mikko Vilpponen avatar  avatar Kibahop avatar  avatar

Forkers

andrewmrquinn

net-snmp-systemd-service-status's Issues

Disabled units

Hello, thanks for the script, but there is a problem:
if the unit is disabled, then it is not displayed in the systemctl list-units list. I modified a little for myself - added one more loop before the main one:

lines = subprocess.check_output(["/bin/systemctl", "list-units", "-a", "-t", "service", "--no-legend"]).decode("UTF-8").split("\n")
lines_disabled = subprocess.check_output(["/bin/systemctl", "list-unit-files", "-a", "-t", "service", "--no-legend"]).decode("UTF-8").split("\n")
        for line in lines_disabled:
            if line and not "@." in line:
               result = re.search(r"^(.+)\.service\s+(\w+).*$", line)
               service_name = result.group(1)
               service_mode = result.group(2)
               if service_mode == 'disabled':
                    service_mode = 3
                    service_oid = self.create_oid(service_name)
                    self.data[service_oid] = ('integer', service_mode, service_name)
                    oids.append(service_oid)

        for line in lines:
            if line and not "@." in line:
            ...

I think a good programmer will make it more beautiful

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.