Reporting end of license on temp products with Chocolatey, Python, Telegraf and Grafana

fabien lenoir
Sep 11, 2018 · 3 min read
source :

I must say that I try to organize myself as well as I can to facilitate the management of the software and hardware I handle, but sometimes I go through the renewal dates of the temporary licenses that we use as part of the development of our applications.

As we have just implemented the Grafana suite with Telegraf agents, I thought it would be nice to be able to automate the alerts on the end of the license.

The alert system at Grafana is quite recent and needs to be improved. For the moment, we have to go through “graphs”.

The objective is therefore to retrieve a date from a text file, to modify it to be processed in a script that will compare this date with the current date. The exit is done in the form of remaining days.

We then integrate in telegraf, a regular call to this script (a bash windows) and we retrieve the information in Telegraf with an alert if the value is less than 5 days.

The first thing to do is to configure the machine that hosts this famous software with temporary link and the telegraf agent. This is Windows 2012 r2 so we start by installing Chocolatey and then install python and create our script.

Ok let’s go ,

First of all, open CMD as admin and paste the Chocolatey install line below

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString(''))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

then python ( here it will be Python 3.7 but you can specify a version)

choco install python -y

You need to close and re-open CMD as admin in order to have Python avalable in your $PATH

Ok so now we need to create a Python file

# -*- coding: utf-8 -*
from time import strptime
from datetime import date
import datetime
# get today's date and reformat it
today =
today =str(today)
YY = today[:4]
MM = today[5:7]
DD = today[8:10]
todate = date(int(YY),int(MM),int(DD))# get license_date from real txt like file use by software
def get_license_date():
with open('C:\\batonSites\\Bin\\interrad.lic', 'r', encoding='utf-8') as f:
line = f.readline()
# print(line[35:46])
return line[34:46]
lic_day = get_license_date()[:2]
lic_mon = get_license_date()[3:6]
lic_year = get_license_date()[7:11]
# reformat month as a number
lic_month = strptime(lic_mon,'%b').tm_mon
lic_month = str(lic_month)# handle date or month with zero number
if lic_day[:1] == '0':
lic_day = lic_day[1:]
if lic_month[:1] == '0':
lic_month = lic_month[1:]
# print(lic_day, lic_month, lic_year)lic_date = date(int(lic_year),int(lic_month),int(lic_day))# substract dates to find resulting days before end of license
res = str(lic_date-todate)

Please excuse if the code is not as clean as it could be, this is subject to improvment. If you copy and paste, be careful at indentation.

So now, we have to update our ‘telegraf.conf’ file

locate the file in C:\Program Files\telegraf and add

[[inputs.exec]]commands = ["powershell python C:\\\\"]timeout = "6h"name_override  = "checklicense"data_format = "value"data_type = "integer"

Here i just ask telegraf to lauch the python script each 6 hours. I use powershell but it should also run on CMD.

Then restart the telegraf service

We now need to add this new data to Grafana. Add a panel and set the metrics depending on your host. Here it remains 42 days before end of license.

The create the alert when number of days is below 5

That’s it.

fabien lenoir

Written by

#Angevin délocalisé,passionné de #code, #python, etc…,

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade