From 27b2efbc959b488cf0b7073ee62267c8a4e2e027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Stocki?= Date: Thu, 12 Dec 2019 14:22:16 +0100 Subject: [PATCH] recreate cache if cannot read --- PVE-Zabbix.py | 2 +- PVEApi.py | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/PVE-Zabbix.py b/PVE-Zabbix.py index 2df3864..0125d00 100755 --- a/PVE-Zabbix.py +++ b/PVE-Zabbix.py @@ -5,7 +5,7 @@ from VM import VM from Storage import Storage USERNAME = "zabbix@pve" -PASSWORD = "KbCurGdt82NTgBye" +PASSWORD = "zabbix" HOST = "localhost:8006" conn = PVEApi(HOST, USERNAME, PASSWORD) diff --git a/PVEApi.py b/PVEApi.py index 20d797e..6867bd0 100755 --- a/PVEApi.py +++ b/PVEApi.py @@ -53,10 +53,15 @@ class PVEApi(object): Proxmox says ticket is valid for 2 hours. For safety we use 1 hour :return: Boolean value of True if ticket is considered expired. False if opposite. """ - with open(CACHEFILE, 'r') as f: - data = json.load(f) - timestamp = data['timestamp'] - return True if time.time() - timestamp > 3600 else False + try: + with open(CACHEFILE, 'r') as f: + data = json.load(f) + timestamp = data['timestamp'] + return True if time.time() - timestamp > 3600 else False + except: + self.csrftoken = get_auth(self.host, username, password)[0] + self.ticket = get_auth(self.host, username, password)[1] + self.write_ticketcache() def write_ticketcache(self):