From f2604e751abe43c2cec773194172ac90b9f89a98 Mon Sep 17 00:00:00 2001 From: Jean Paul Galea Date: Tue, 8 Sep 2015 14:15:36 +0200 Subject: [PATCH] Modify ykval-munin-vallatency plugin. - use hostname (+ port if any) for graph name. i.e. if we have a sync URL: https://api.yubico.com:8080/wsapi/2.0/sync instead of having "api" as name, we have "api.yubico.com:8080". - also avoid using regex and use parse_url() from php core instead. --- ykval-common.php | 20 ++++++++++++++++++++ ykval-munin-vallatency.php | 16 +++++++--------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/ykval-common.php b/ykval-common.php index bfff702..7144458 100644 --- a/ykval-common.php +++ b/ykval-common.php @@ -362,3 +362,23 @@ function total_time ($url) return $total_time; } + +/** + * Return the hostname, and if defined the port, for a given URL. + * + * @argument $url string + * @return string|bool short name or false on failure + */ +function short_name ($url) +{ + if (($url = parse_url($url)) === FALSE) + return false; + + if (array_key_exists('host', $url) === FALSE || $url['host'] === NULL) + return false; + + if (array_key_exists('port', $url) === TRUE && $url['port'] !== NULL) + return $url['host'] . ':' . $url['port']; + + return $url['host']; +} diff --git a/ykval-munin-vallatency.php b/ykval-munin-vallatency.php index 2ce7ae2..51466b5 100755 --- a/ykval-munin-vallatency.php +++ b/ykval-munin-vallatency.php @@ -37,20 +37,18 @@ set_include_path(implode(PATH_SEPARATOR, array( require_once 'ykval-config.php'; require_once 'ykval-common.php'; -function url2shortname ($url) +$urls = $baseParams['__YKVAL_SYNC_POOL__']; + +$shortnames = array_map("short_name", $urls); +foreach($shortnames as $val) { - if (preg_match("/^[^\/]+\/\/([a-z0-9-]+)/", $url, $name) == 0) + if ($val === FALSE) { - echo "Cannot match URL hostname: " . $url . "\n"; + echo "Cannot parse URL from sync pool list\n"; exit(1); } - - return $name[1]; } -$urls = $baseParams['__YKVAL_SYNC_POOL__']; -$shortnames = array_map("url2shortname", $urls); - if ($argc == 2 && strcmp($argv[1], "autoconf") == 0) { print "yes\n"; @@ -80,7 +78,7 @@ if ($argc == 2 && strcmp($argv[1], "config") == 0) echo "multigraph ykval_vallatency\n"; foreach ($urls as $url) { - $shortname = url2shortname($url); + $shortname = short_name($url); if (($total_time = total_time($url)) === FALSE) $total_time = 'error';