#!/usr/bin/python
# coding=utf-8
# Copyright (C) 2021:
#     Linexos, info@linexos.fr


import os, sys, time
from datetime import datetime
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-W', '--warning', help='Warning threshold', default=1, type=float)
parser.add_argument('-C', '--critical', help='Critical theshold', default=10, type=float)

args = parser.parse_args()

if (args.critical and args.critical < 0):
    print("Error: -C Critical cannot be negative")
    sys.exit(3)
elif (args.warning and args.warning < 0):
    print("Error: -W Warning cannot be negative")
    sys.exit(3)

UPDATE = 'sudo apt-get update'
os.popen(UPDATE)

LENUPDATESECU = 'apt-get upgrade -s| grep ^Inst |grep "Debian-Security"| wc -l'
NBUPDATE = int(os.popen(LENUPDATESECU))

# Test the value
if (NBUPDATE >= args.critical):
    print("CRITICAL : %s Security update available | SecurityUpdate=%s" % (NBUPDATE, NBUPDATE))
    status_code = 2
elif (NBUPDATE >= args.warning):
    print("WARNING : %s Security update available | SecurityUpdate=%s" % (NBUPDATE, NBUPDATE))
    status_code = 1
else:
    print("OK : No security update available | SecurityUpdate=%s" % (NBUPDATE))
    status_code = 0

sys.exit(status_code)
