[monitoring-plugins] GMT expiry display

Sven Nierlein <[email protected]>
Newsgroups gmane.network.nagios.cvs
Message-ID <20161107220013.8C03920083C8__26338.415061082$1478556046$gmane$org@orwell.monitoring-plugins.org>
    Module: monitoring-plugins
    Branch: master
    Commit: 2f2b2f1987d63cb4b31b2ef4c95cf8c8ab1d83c6
    Author: Peter (pir) Radcliffe <[email protected]>
 Committer: Sven Nierlein <[email protected]>
      Date: Wed Nov 25 13:58:30 2015 +0000
       URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=2f2b2f1

GMT expiry display

Change solution to display GMT time in the local display format with
the offset number of hours from GMT to be clear about what timezone
this is if the local display format does not include offset.

---

 plugins/sslutils.c         | 11 ++++++++++-
 plugins/tests/check_http.t |  8 ++++----
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/plugins/sslutils.c b/plugins/sslutils.c
index 6437289..b412ef3 100644
--- a/plugins/sslutils.c
+++ b/plugins/sslutils.c
@@ -197,6 +197,7 @@ int np_net_ssl_check_cert(int days_till_exp_warn, int days_till_exp_crit){
 	X509_NAME *subj=NULL;
 	char timestamp[50] = "";
 	char cn[MAX_CN_LENGTH]= "";
+	char *tz;
 	
 	int cnlen =-1;
 	int status=STATE_UNKNOWN;
@@ -267,7 +268,15 @@ int np_net_ssl_check_cert(int days_till_exp_warn, int days_till_exp_crit){
 	tm_t = timegm(&stamp);
 	time_left = difftime(tm_t, time(NULL));
 	days_left = time_left / 86400;
-	strftime(timestamp, 50, "%F %R %z/%Z", localtime(&tm_t));
+	tz = getenv("TZ");
+	setenv("TZ", "GMT", 1);
+	tzset();
+	strftime(timestamp, 50, "%c %z", localtime(&tm_t));
+	if (tz)
+		setenv("TZ", tz, 1);
+	else
+		unsetenv("TZ");
+	tzset();
 
 	if (days_left > 0 && days_left <= days_till_exp_warn) {
 		printf (_("%s - Certificate '%s' expires in %d day(s) (%s).\n"), (days_left>days_till_exp_crit)?"WARNING":"CRITICAL", cn, days_left, timestamp);
diff --git a/plugins/tests/check_http.t b/plugins/tests/check_http.t
index e72d243..5984d48 100755
--- a/plugins/tests/check_http.t
+++ b/plugins/tests/check_http.t
@@ -186,21 +186,21 @@ SKIP: {
 
 	$result = NPTest->testCmd( "$command -p $port_https -S -C 14" );
 	is( $result->return_code, 0, "$command -p $port_https -S -C 14" );
-	is( $result->output, 'OK - Certificate \'Ton Voon\' will expire on Sun Mar  3 21:41:28 2019.', "output ok" );
+	is( $result->output, 'OK - Certificate \'Ton Voon\' will expire on Sun Mar  3 21:41:28 2019 +0000.', "output ok" );
 
 	$result = NPTest->testCmd( "$command -p $port_https -S -C 14000" );
 	is( $result->return_code, 1, "$command -p $port_https -S -C 14000" );
-	like( $result->output, '/WARNING - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(Sun Mar  3 21:41:28 2019\)./', "output ok" );
+	like( $result->output, '/WARNING - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(Sun Mar  3 21:41:28 2019 \+0000\)./', "output ok" );
 
 	# Expired cert tests
 	$result = NPTest->testCmd( "$command -p $port_https -S -C 13960,14000" );
 	is( $result->return_code, 2, "$command -p $port_https -S -C 13960,14000" );
-	like( $result->output, '/CRITICAL - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(Sun Mar  3 21:41:28 2019\)./', "output ok" );
+	like( $result->output, '/CRITICAL - Certificate \'Ton Voon\' expires in \d+ day\(s\) \(Sun Mar  3 21:41:28 2019 \+0000\)./', "output ok" );
 
 	$result = NPTest->testCmd( "$command -p $port_https_expired -S -C 7" );
 	is( $result->return_code, 2, "$command -p $port_https_expired -S -C 7" );
 	is( $result->output,
-		'CRITICAL - Certificate \'Ton Voon\' expired on Thu Mar  5 00:13:16 2009.',
+		'CRITICAL - Certificate \'Ton Voon\' expired on Thu Mar  5 00:13:16 2009 +0000.',
 		"output ok" );
 
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.