Skip to content
Snippets Groups Projects
Commit b403cdea authored by root's avatar root
Browse files

Added check_cpu_multicore.

parent 77681c96
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/python3
# Author: Devon Merner (dmerner)
# Date: April 21, 2022
# Purpose: To parse and monitor multicore CPU statistics on a linux machine
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_LABEL = {
STATE_OK : "OK",
STATE_WARNING : "WARNING",
STATE_CRITICAL : "CRITICAL",
STATE_UNKNOWN : "UNKNOWN"
}
# Default exit code
EXIT_CODE = STATE_OK
import sys
import argparse
import subprocess
import json
import shutil
parser = argparse.ArgumentParser()
parser.add_argument("-d", action="store_true", dest="debug", help="Print debug information")
options = parser.parse_args()
def main():
# First check whether the command exists
if not shutil.which('mpstat'):
print("mpstat command not found. Is the sysstat package installed? |")
sys.exit(STATE_OK)
# mpstat -P ALL -o JSON
fsout = subprocess.Popen(['mpstat', '-P', 'ALL', '-o', 'JSON'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = fsout.stdout.read()
data = json.loads(stdout)
PERFDATA = ""
OUTPUT_STRING = ""
for core in data['sysstat']['hosts'][0]['statistics'][0]['cpu-load']:
if 'all' in core['cpu']:
OUTPUT_STRING += f"Total CPU Usage: {round(100 - core['idle'], 2)}%, CPUs: {data['sysstat']['hosts'][0]['number-of-cpus']}, Arch: {data['sysstat']['hosts'][0]['machine']} "
for key in core:
if key == 'cpu':
continue
PERFDATA += f" cpu_{core['cpu']}-{key}={core[key]}"
print(OUTPUT_STRING + "|" + PERFDATA)
# Critical/Warning Think Logic
if EXIT_CODE == STATE_CRITICAL:
sys.exit(STATE_CRITICAL)
elif EXIT_CODE == STATE_WARNING:
sys.exit(STATE_WARNING)
else:
sys.exit(STATE_OK)
if __name__ == '__main__':
main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment