Add unixtime option

Add an option to return the expiration date in unixtime (time since
epoch).

  ./check_expire -H domain -u

It's particularly useful for alerting or monitoring systems that do
their own calculation based on the metric they receive (and not based on
exit codes).

For instance you can use the following in zabbix 5.4:
- item: check_expire["-H","{HOST.CONN}","-u"]
- trigger: last(/check_expire["-H","{HOST.CONN}","-u"])-now()<2592000

Where 2592000 is 30 days in seconds.
This commit is contained in:
Faustin Lammler 2021-07-06 12:22:38 +02:00
parent 5c8a6af546
commit 342f89844b
Signed by: faust
GPG Key ID: 390A2F27832A5C79
2 changed files with 26 additions and 12 deletions

View File

@ -10,6 +10,7 @@ check_expire follows the usual Nagios rules. The options are:
* -c: critical threshold in days
* -w: warning threshold in days
* -v: verbose details in output
* -u: unixtime output
## Installation
@ -57,6 +58,17 @@ object Host "bortzmeyer-org" {
}
```
## Zabbix configuration
For monitoring systems that do not rely on exit codes but on calculation mechanism based on the metric they receive you can use the `-u` option that will only return the expiration date in unixtime format.
For instance, you can use the following in zabbix 5.4:
* item: `check_expire["-H","{HOST.CONN}","-u"]`
* trigger (30 days before expiration): `last(/check_expire["-H","{HOST.CONN}","-u"])-now()<2592000`
Where `2592000` is the number of seconds in 30 days.
## License
GPL. See LICENSE.
@ -68,5 +80,3 @@ Stéphane Bortzmeyer <stephane+chapril@bortzmeyer.org>.
## Reference site
https://forge.chapril.org/bortzmeyer/check_expire Use the Gitea issue tracker to report bugs or wishes.

View File

@ -42,10 +42,11 @@ verbose = False
domain = None
critical_t = datetime.timedelta(days=2)
warning_t = datetime.timedelta(days=7)
unixtime = False
# TODO implement timeout for HTTPS requests. -t option. Does Requests allow it?
def usage(msg=None):
print("Usage: %s -H domain-name [-c critical -w warning]" % sys.argv[0], end="")
print("Usage: %s -H domain-name [-c critical -w warning -u unixtime]" % sys.argv[0], end="")
if msg is not None and msg != "":
print(" (%s)" % msg)
else:
@ -80,8 +81,8 @@ def ok(msg=None):
sys.exit(STATE_OK)
try:
optlist, args = getopt.getopt (sys.argv[1:], "c:hH:vVw:",
["critical=", "expiration", "help", "verbose", "version", "warning="])
optlist, args = getopt.getopt (sys.argv[1:], "c:hH:vVw:u",
["critical=", "expiration", "help", "verbose", "version", "warning=", "unixtime"])
for option, value in optlist:
if option == "--critical" or option == "-c":
critical_t = datetime.timedelta(days=int(value))
@ -97,6 +98,8 @@ try:
sys.exit(STATE_OK)
elif option == "--warning" or option == "-w":
warning_t = datetime.timedelta(days=int(value))
elif option == "--unixtime" or option == "-u":
unixtime = True
else:
# Should never occur, it is trapped by getopt
print("Unknown option %s" % option)
@ -136,6 +139,9 @@ for event in rdap["events"]:
print("No recognized format for datetime \"%s\"" % event["eventDate"])
sys.exit(STATE_UNKNOWN)
now = datetime.datetime.now(tz=UTCINFO)
if unixtime == True:
print(int(expiration.strftime("%s")))
sys.exit(STATE_OK)
if expiration < now:
error("domain %s is already expired (%s ago)" % (domain, (now-expiration)))
rest = expiration-now
@ -146,5 +152,3 @@ for event in rdap["events"]:
else:
ok("expires in %s." % (rest))
error("No expiration found")