#!/usr/bin/python3
# Import libraries
import sys, getopt, requests, urllib3, json
urllib3.disable_warnings()
from datetime import datetime, time, timezone
# Configuration variables
fqdn_pbs = ''
api_token_pbs = ''
debug = False
# Verify and handle command line arguments
options, remainder = getopt.gnu_getopt(sys.argv[1:], 'hdf:t:')
for opt, arg in options:
if opt == '-h':
print ('check_pbs.py -d (debug) -f <PBS server fqdn> -t <API token: <username>@pbs!<api id>:<api secret>>')
sys.exit()
if opt == '-d':
debug = True
if opt == '-f':
fqdn_pbs = arg
elif opt == '-t':
api_token_pbs = 'PBSAPIToken=' + arg
if fqdn_pbs == '' or api_token_pbs == '':
print ('check_pbs.py -d (debug) -f <PBS server fqdn> -t <API token: <username>@pbs!<api id>:<api secret>>')
sys.exit(1)
# Set some global variables
taskoutput=[]
processfailure=0
# Function for PBS api query
def get_pbs_tasks(fqdn, status, apitoken):
totaltasks=0
midnight = datetime.combine(datetime.today(), time.min)
resp=requests.get("https://" + fqdn + ":8007/api2/json/nodes/localhost/tasks?limit=0&statusfilter=" + status + "&since=" + midnight.strftime('%s'),verify=False,timeout=5,headers={'Authorization': apitoken})
if resp.status_code == 401:
taskoutput.append("Authentication failed for https://" + fqdn + ":8007")
taskoutput.append('')
return [False, 0]
if resp.status_code == 200:
jsondata = resp.json() # Check the JSON Response Content documentation below
if debug:
print(json.dumps(jsondata, indent=2)) # Print complete json data for debugging purposes
print('')
for i in jsondata['data']:
taskoutput.append('worker_id: '+i['worker_id'])
taskoutput.append('worker_type: '+i['worker_type'])
taskoutput.append('user: '+i['user'])
taskoutput.append('type: error')
taskoutput.append('status: '+i['status'])
taskoutput.append('starttime: '+str(datetime.fromtimestamp(i['starttime'])))
taskoutput.append('endtime: '+str(datetime.fromtimestamp(i['endtime'])))
taskoutput.append('')
totaltasks+=1
return [True, totaltasks]
if resp.status_code != 200:
taskoutput.append("Failed to access or process https://" + fqdn + ":8007")
taskoutput.append('')
return [False, 0]
### Process pbs server tasks
taskoutput.append('')
taskoutput.append('Today\'s unsuccessfull tasks at ' + fqdn_pbs + ':')
taskoutput.append('')
pbs_errors=0
pbs_warnings=0
result = get_pbs_tasks(fqdn_pbs, "error", api_token_pbs)
if result[0] == True:
pbs_errors+=result[1]
else:
processfailure+=1
result = get_pbs_tasks(fqdn_pbs, "warning", api_token_pbs)
if result[0] == True:
pbs_warnings+=result[1]
else:
processfailure+=1
taskoutput.append('errors: '+str(pbs_errors)+' warnings: '+str(pbs_warnings))
taskoutput.append('')
###Print and return Nagios status and performance data
# Unknown in case of process failures
if processfailure>0:
print('SERVICE STATUS: UNKNOWN | errors='+str(pbs_errors)+' warnings='+str(pbs_warnings))
for outputline in taskoutput:
print(outputline)
sys.exit(3)
# OK If no errors and warning
if pbs_errors==0 and pbs_warnings==0:
print('SERVICE STATUS: OK | errors='+str(pbs_errors)+' warnings='+str(pbs_warnings))
for outputline in taskoutput:
print(outputline)
sys.exit(0)
# Warning if no errors and one or more warnings
if pbs_errors==0 and pbs_warnings>0:
print('SERVICE STATUS: WARNING | errors='+str(pbs_errors)+' warnings='+str(pbs_warnings))
for outputline in taskoutput:
print(outputline)
sys.exit(1)
# Error in case of one or more errors
if pbs_errors>0:
print('SERVICE STATUS: ERROR | errors='+str(pbs_errors)+' warnings='+str(pbs_warnings))
for outputline in taskoutput:
print(outputline)
sys.exit(2)