Last active
May 25, 2018 10:29
-
-
Save kennethgillen/bbaeb236d7945344407d6bb3bde56bc9 to your computer and use it in GitHub Desktop.
Check_MK local check for OMERO server which returns count of unique users logged-in to an OMERO server
This file contains hidden or 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 | |
from commands import getstatusoutput | |
import datetime | |
import socket | |
# [email protected] | |
# This code is a Check_MK local check, designed to query an OMERO server via `omero sessions who` | |
# and return the number of logged in OMERO users to Check_MK. | |
# Check_MK reference: https://mathias-kettner.de/checkmk_localchecks.html | |
# | |
# License https://creativecommons.org/licenses/by/4.0/ | |
# | |
# run command / exit status code from: https://pymotw.com/2/commands/ (license by-nc-sa/3.0) | |
# Customise these following vars for your OMERO deployment | |
# Full path to OMERO CLI | |
omero_command = '/opt/omero/server/OMERO.server/bin/omero' | |
# OMERO operating system user to run OMERO CLI | |
os_user_cli = 'omero-server' | |
# OMERO user account username this script will use to log in to the OMERO server. | |
# Password can be in environment variable `OMERO_PASSWORD` or coded into the variable | |
# `$cmd` below. | |
omero_user_cli = 'monitoring' | |
# A file which should be readable by root only, containing the | |
# password of the OMERO account which will be used to log into OMERO | |
omero_user_cli_pwd_file = '/root/omero_user_cli_password.txt' | |
# The OMERO user account username of the omero "public user" to be | |
# removed from "active sessions". | |
# If you have no public user defined, safe to leave this as | |
# 'PUBLIC'. Do not set to empty. | |
omero_public_user = 'PUBLIC' | |
# Read the password for the monitoring user from a file | |
# only readable by the root user | |
file = open(omero_user_cli_pwd_file, "r") | |
for line in file: | |
monitoring_password = line.strip() | |
# Run bin/omero sessions who, removing the non-human users and pretty-printing. | |
# Count only unique usernames seen. | |
cmd = 'OMERO_PASSWORD=\"' + monitoring_password + '\" HOME=\"$(getent passwd %s | cut -d: -f6)\" sudo -E -u %s sh -c \'%s -s localhost --user %s ' % (os_user_cli, os_user_cli, omero_command, omero_user_cli) + ' ' + ' sessions who | grep -Ev "(root|rows|guest|\-\+\-|agent|%s|%s)" | awk \"{ print \$1 }\" | sort | uniq | wc -l\'' % (omero_public_user, omero_user_cli) | |
status, text = getstatusoutput(cmd) | |
exit_code = status >> 8 # high byte | |
debug = False | |
if debug: | |
print 'Ran: "%s"' % cmd | |
print 'Status: x%04x' % status | |
# Output from command: | |
print 'Output: %s' % text | |
print 'Just the count of users?: %s' % text.split('\n', 1)[1] | |
exit = '%d' % (exit_code) | |
# above is a string | |
if debug: | |
exit = '1' # debugging, testing with other exit codes | |
print 'Just the exit code: %s' % (exit) | |
# Example format: | |
# echo "0 omero-user-sessions count=${user_sessions} OK - ${user_sessions} OMERO users logged in at $(date)" | |
# The number of logged-in users is in the var 'text' | |
# If exit code 0, then return OK to check_mk along with user count | |
# string comparison | |
if exit == '0': | |
# Okay, we've likely got good output from our command. Let's populate the vars we require from command output. | |
count_of_users = text.split('\n', 1)[1] | |
# Exit code of OMERO command is == status to check_mk, i.e. | |
# if the command worked, check is OK. | |
print '%s omero-user-sessions-unique_users count=%s OK - %s total unique OMERO (non system, public or monitoring) user(s) connected at %s, output: %s' % (exit, count_of_users, count_of_users, datetime.datetime.now().strftime('%c'), text) | |
else: | |
print '%s omero-user-sessions-unique_users count=0 CRITICAL - Problem with the omero user sessions command, investigate script in /usr/share/check-mk-agent/local on host %s ' % (exit, socket.gethostname()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment