Skip to content

Instantly share code, notes, and snippets.

@ChrisCarini
Last active November 8, 2018 00:18
Show Gist options
  • Save ChrisCarini/7f1f32148de84bad129bab451c5ccfd5 to your computer and use it in GitHub Desktop.
Save ChrisCarini/7f1f32148de84bad129bab451c5ccfd5 to your computer and use it in GitHub Desktop.
A simple script to send an email when the JetBrains Floating License Server exceeds a given threshold.
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * username command to be executed
MAILTO="YOUR_EMAIL@gmail.com"
1 9-17 * * 1-5 /usr/bin/python /path/to/JetBrainsFloatingLicenseServerAlerter.py
MAILTO=""
#!/usr/bin/python
import sys
import requests
import datetime
import smtplib
from email.mime.text import MIMEText
##
# Configuration Values
##
# Parameters for Emailing
from_email = 'FROM_EMAIL@gmail.com'
to_email = ['TO_EMAIL_1@gmail.com', 'TO_EMAIL_2@outlook.com', 'TO_EMAIL_3@yahoo.com']
# Threshold for alerting - once the number of used licenses exceeds this threshold, the script will alert.
alert_threshold = 2000
# URL for the API of the license server
apiUrl = "http://HOSTNAME/licenseServer/reportApi"
dateStr = datetime.datetime.now().strftime("%Y-%m-%d")
print "sys.argv leng: ", len(sys.argv)
if len(sys.argv) == 3:
start_date = sys.argv[1]
end_date = sys.argv[2]
else:
start_date = dateStr
end_date = dateStr
dataObj = {
'start': start_date,
'end': end_date,
'token': '12345',
'granularity': '0', # 0 = hourly, 1 = daily
}
r = requests.post(apiUrl, data=dataObj)
if (r.status_code is 200):
resp = r.json()['License Usage Hourly']
emailBody = ""
maxCount = 0
for respDict in resp:
# print respDict['key']
if (respDict['key'].find("IntelliJ IDEA Ultimate") != -1):
count = respDict['count']
if (count > maxCount):
maxCount = count
tmpStr = "{0} - {1}".format(respDict['time'], count)
emailBody += tmpStr + "\n"
print tmpStr
tmpStr = "Max Usage: {0}".format(maxCount)
emailBody += tmpStr + "\n"
print tmpStr
if (maxCount >= alert_threshold):
print "Max Count Exceeded {0}!!! Sending Email!".format(alert_threshold)
msg = MIMEText(emailBody)
msg['Subject'] = "[WARNING] JetBrains Floating License Server Exceeded Threshold"
msg['From'] = from_email
msg['To'] = ", ".join(to_email)
s = smtplib.SMTP('localhost')
s.sendmail(from_email, to_email, msg.as_string())
s.quit()
print "Done Sending Email."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment