From 853c403972663c06bee57390f8708738fff95464 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20=C3=85kre=20Solberg?= <andreas.solberg@uninett.no>
Date: Wed, 11 Feb 2009 10:08:15 +0000
Subject: [PATCH] Take time of every operation and report

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1258 44740490-163a-0410-bde0-09ae8108e29a
---
 modules/ldapstatus/lib/LDAPTester.php         | 36 +++++++++++++------
 .../ldapstatus/templates/ldapsinglehost.php   |  2 +-
 modules/ldapstatus/www/index.php              |  2 +-
 3 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/modules/ldapstatus/lib/LDAPTester.php b/modules/ldapstatus/lib/LDAPTester.php
index afdb46af1..1c8255867 100644
--- a/modules/ldapstatus/lib/LDAPTester.php
+++ b/modules/ldapstatus/lib/LDAPTester.php
@@ -39,7 +39,7 @@ class sspmod_ldapstatus_LDAPTester {
 		if (count($err) > 0) {
 			return array(FALSE, 'Missing: ' . join(', ', $err));
 		}
-		return array(TRUE, NULL);	
+		return array(TRUE, '');	
 	}
 	
 	
@@ -71,10 +71,14 @@ class sspmod_ldapstatus_LDAPTester {
 		if ($errno) {
 			return array(FALSE, $errno . ':' . $errstr . ' [' . $host . ':' . $port . ']');
 		} else {		
-			return array(TRUE,NULL);
+			return array(TRUE,'');
 		}
 	}
 	
+	public function getTimeText($time) {
+		return 'Operation took ' . ceil($time*1000) . ' ms';
+	}
+	
 	public function test() {
 		$start = microtime(TRUE);
 		
@@ -111,8 +115,9 @@ class sspmod_ldapstatus_LDAPTester {
 	
 
 	
-	
+		$laststart = microtime(TRUE);
 		$result['ping'] = $this->phpping($url['host'], $port);
+		$result['ping'][1] .= $this->getTimeText(microtime(TRUE) - $laststart); 
 	
 		if (!$result['ping'][0]) {
 			$result['time'] = microtime(TRUE) - $start;
@@ -122,12 +127,15 @@ class sspmod_ldapstatus_LDAPTester {
 		
 		// LDAP Connect
 		try {
+			$laststart = microtime(TRUE);
 			$ldap = new SimpleSAML_Auth_LDAP($this->orgconfig['hostname'], 
 				(array_key_exists('enable_tls', $this->orgconfig) ? $this->orgconfig['enable_tls'] : FALSE), 
 				$this->debug);
 			
 			if ($ldap->getLastError()) throw new Exception('LDAP warning: ' . $ldap->getLastError());
-			$result['connect'] = array(TRUE,NULL);
+			$result['connect'] = array(TRUE,'');
+			$result['connect'][1] .= $this->getTimeText(microtime(TRUE) - $laststart); 
+			
 		} catch (Exception $e) {
 			$this->log('ldapstatus: Connect error() [' .$this->orgconfig['hostname'] . ']: ' . $e->getMessage());
 			$result['connect'] = array(FALSE,$e->getMessage());
@@ -138,14 +146,16 @@ class sspmod_ldapstatus_LDAPTester {
 		// Bind as admin user
 		if (isset($this->orgconfig['adminUser'])) {
 			try {
+				$laststart = microtime(TRUE);
 				$this->log('ldapstatus: Admin bind() [' .$this->orgconfig['hostname'] . ']');
 				$success = $ldap->bind($this->orgconfig['adminUser'], $this->orgconfig['adminPassword']);
 				if ($ldap->getLastError()) throw new Exception('LDAP warning: ' . $ldap->getLastError());
 				if ($success) {
-					$result['adminBind'] = array(TRUE,NULL);
+					$result['adminBind'] = array(TRUE,'');
 				} else {
 					$result['adminBind'] = array(FALSE,'Could not bind()' );
 				}
+				$result['adminBind'][1] .= $this->getTimeText(microtime(TRUE) - $laststart); 
 			} catch (Exception $e) {
 				$this->log('admin Bind() error:' . $e->getMessage());
 				$result['adminBind'] = array(FALSE,$e->getMessage());
@@ -158,9 +168,11 @@ class sspmod_ldapstatus_LDAPTester {
 		$eppn = 'asdasdasdasd@feide.no';
 		// Search for bogus user
 		try {
+			$laststart = microtime(TRUE);
 			$dn = $ldap->searchfordn($this->orgconfig['searchbase'], 'eduPersonPrincipalName', $eppn, TRUE);
 			if ($ldap->getLastError()) throw new Exception('LDAP warning: ' . $ldap->getLastError());
-			$result['ldapSearchBogus'] = array(TRUE,NULL);
+			$result['ldapSearchBogus'] = array(TRUE,'');
+			$result['ldapSearchBogus'][1] .= $this->getTimeText(microtime(TRUE) - $laststart); 
 		} catch (Exception $e) {
 			$this->log('LDAP Search bogus:' . $e->getMessage());
 			$result['ldapSearchBogus'] = array(FALSE,$e->getMessage());
@@ -176,9 +188,11 @@ class sspmod_ldapstatus_LDAPTester {
 
 			// Try to search for DN of test account
 			try {
+				$laststart = microtime(TRUE);
 				$dn = $ldap->searchfordn($this->orgconfig['searchbase'], 'eduPersonPrincipalName', $this->orgconfig['testUser']);
 				if ($ldap->getLastError()) throw new Exception('LDAP warning: ' . $ldap->getLastError());
-				$result['ldapSearchTestUser'] = array(TRUE,NULL);
+				$result['ldapSearchTestUser'] = array(TRUE,'');
+				$result['ldapSearchTestUser'][1] .= $this->getTimeText(microtime(TRUE) - $laststart); 
 			} catch (Exception $e) {
 				$this->log('LDAP Search test account:' . $e->getMessage());
 				$result['ldapSearchTestUser'] = array(FALSE,$e->getMessage());
@@ -187,19 +201,21 @@ class sspmod_ldapstatus_LDAPTester {
 			}
 			
 			if ($ldap->bind($dn, $this->orgconfig['testPassword'])) {
-				$result['ldapBindTestUser'] = array(TRUE,NULL);
+				$result['ldapBindTestUser'] = array(TRUE,'');
 				
 			} else {
 				$this->log('LDAP Test user bind() failed...');
-				$result['ldapBindTestUser'] = array(FALSE,NULL);
+				$result['ldapBindTestUser'] = array(FALSE,'');
 				$result['time'] = microtime(TRUE) - $start;
 				return $result;
 			}
 	
 			try {
+				$laststart = microtime(TRUE);
 				$attributes = $ldap->getAttributes($dn, $this->orgconfig['attributes']);
 				if ($ldap->getLastError()) throw new Exception('LDAP warning: ' . $ldap->getLastError());
-				$result['ldapGetAttributesTestUser'] = array(TRUE,NULL);
+				$result['ldapGetAttributesTestUser'] = array(TRUE,'');
+				$result['ldapGetAttributesTestUser'][1] .= $this->getTimeText(microtime(TRUE) - $laststart); 
 			} catch(Exception $e) {
 				$this->log('LDAP Test user attributes failed:' . $e->getMessage());
 				$result['ldapGetAttributesTestUser'] = array(FALSE,$e->getMessage());
diff --git a/modules/ldapstatus/templates/ldapsinglehost.php b/modules/ldapstatus/templates/ldapsinglehost.php
index 23e47a096..019516272 100644
--- a/modules/ldapstatus/templates/ldapsinglehost.php
+++ b/modules/ldapstatus/templates/ldapsinglehost.php
@@ -43,7 +43,7 @@ function presentRes($restag) {
 		$res = $t->data['res'][$restag];
 		if ($res[0]) {	
 			echo('<img style="float: right" src="/' . $t->data['baseurlpath'] . 'resources/icons/accept.png" />');
-			echo('OK');
+			echo('OK: ' . $res[1]);
 		} else {
 			echo('<img style="float: right" src="/' . $t->data['baseurlpath'] . 'resources/icons/gn/stop-l.png" />');
 			echo($res[1]);
diff --git a/modules/ldapstatus/www/index.php b/modules/ldapstatus/www/index.php
index 4b0cd3ad5..c6df46b84 100644
--- a/modules/ldapstatus/www/index.php
+++ b/modules/ldapstatus/www/index.php
@@ -114,7 +114,7 @@ $maxtime = $ldapStatusConfig->getValue('maxExecutionTime', 15);
 
 
 if (array_key_exists('orgtest', $_REQUEST)) {
-	$old_error_handler = set_error_handler("myErrorHandler");
+	#$old_error_handler = set_error_handler("myErrorHandler");
 	
 	$tester = new sspmod_ldapstatus_LDAPTester($orgs[$_REQUEST['orgtest']], $debug);
 	$res = $tester->test();
-- 
GitLab