From e2f5a1fdf0ece32422f35251f67b23772659e12f Mon Sep 17 00:00:00 2001 From: Mark Schouten Date: Thu, 20 Aug 2015 12:25:47 +0200 Subject: [PATCH 1/3] Make app-recognition more efficient --- includes/polling/unix-agent.inc.php | 33 ++++++++--------------------- 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/includes/polling/unix-agent.inc.php b/includes/polling/unix-agent.inc.php index a759313a4..cc0ac1fd2 100644 --- a/includes/polling/unix-agent.inc.php +++ b/includes/polling/unix-agent.inc.php @@ -47,32 +47,17 @@ if ($device['os_group'] == 'unix') { list($section, $data) = explode('>>>', $section); list($sa, $sb) = explode('-', $section, 2); - if ($section == 'apache') { - $sa = 'app'; - $sb = 'apache'; + $agentapps = array( + "apache", + "mysql", + "nginx", + "bind", + "tinydns"); + + if (in_array($section, $agentapps)) { + $agent_data['app'][$section] = trim($data); } - if ($section == 'mysql') { - $sa = 'app'; - $sb = 'mysql'; - } - - if ($section == 'nginx') { - $sa = 'app'; - $sb = 'nginx'; - } - - if ($section == 'bind') { - $sa = 'app'; - $sb = 'bind'; - } - - if ($section == 'tinydns') { - $sa = 'app'; - $sb = 'tinydns'; - } - - // if ($section == "drbd") { $sa = "app"; $sb = "drbd"; } if (!empty($sa) && !empty($sb)) { $agent_data[$sa][$sb] = trim($data); } From 30e47b382c4fc41bc637580b3d747870cffa8471 Mon Sep 17 00:00:00 2001 From: Mark Schouten Date: Thu, 20 Aug 2015 12:36:37 +0200 Subject: [PATCH 2/3] Do not include the applications, but enable them in the database. The application poller will run them. Also, reduce code-duplication. --- includes/polling/unix-agent.inc.php | 37 ++++++++++------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/includes/polling/unix-agent.inc.php b/includes/polling/unix-agent.inc.php index cc0ac1fd2..c5f5d4bc4 100644 --- a/includes/polling/unix-agent.inc.php +++ b/includes/polling/unix-agent.inc.php @@ -83,16 +83,6 @@ if ($device['os_group'] == 'unix') { } } - foreach (array_keys($agent_data['app']) as $key) { - if (file_exists("includes/polling/applications/$key.inc.php")) { - if ($debug) { - echo "Including: applications/$key.inc.php"; - } - - include "applications/$key.inc.php"; - } - } - // Processes if (!empty($agent_data['ps'])) { echo 'Processes: '; @@ -111,12 +101,18 @@ if ($device['os_group'] == 'unix') { echo "\n"; } - // Apache - if (!empty($agent_data['app']['apache'])) { - $app_found['apache'] = true; - if (dbFetchCell('SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ?', array($device['device_id'], 'apache')) == '0') { - echo "Found new application 'Apache'\n"; - dbInsert(array('device_id' => $device['device_id'], 'app_type' => 'apache'), 'applications'); + foreach (array_keys($agent_data['app']) as $key) { + if (file_exists("includes/polling/applications/$key.inc.php")) { + if ($debug) { + echo "Enabling $key for $device['hostname'] if not yet enabled\n"; + } + + if (in_array($key, array('apache', 'mysql', 'nginx')) { + if (dbFetchCell('SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ?', array($device['device_id'], $key)) == '0') { + echo "Found new application '$key'\n"; + dbInsert(array('device_id' => $device['device_id'], 'app_type' => $key), 'applications'); + } + } } } @@ -131,15 +127,6 @@ if ($device['os_group'] == 'unix') { } } - // MySQL - if (!empty($agent_data['app']['mysql'])) { - $app_found['mysql'] = true; - if (dbFetchCell('SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ?', array($device['device_id'], 'mysql')) == '0') { - echo "Found new application 'MySQL'\n"; - dbInsert(array('device_id' => $device['device_id'], 'app_type' => 'mysql'), 'applications'); - } - } - // DRBD if (!empty($agent_data['drbd'])) { $agent_data['app']['drbd'] = array(); From 47c87cb7c6d82a05a8e2ab684a0850f5a6c99f12 Mon Sep 17 00:00:00 2001 From: Mark Schouten Date: Thu, 20 Aug 2015 13:26:18 +0200 Subject: [PATCH 3/3] Fix typos (Sorry about that) Replace if($debug){..} with d_echo --- includes/polling/unix-agent.inc.php | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/includes/polling/unix-agent.inc.php b/includes/polling/unix-agent.inc.php index c5f5d4bc4..d68daa816 100644 --- a/includes/polling/unix-agent.inc.php +++ b/includes/polling/unix-agent.inc.php @@ -66,18 +66,14 @@ if ($device['os_group'] == 'unix') { } }//end foreach - if ($debug) { - print_r($agent_data); - } + d_echo($agent_data); include 'unix-agent/packages.inc.php'; include 'unix-agent/munin-plugins.inc.php'; foreach (array_keys($agent_data) as $key) { if (file_exists("includes/polling/unix-agent/$key.inc.php")) { - if ($debug) { - echo "Including: unix-agent/$key.inc.php"; - } + d_echo("Including: unix-agent/$key.inc.php"); include "unix-agent/$key.inc.php"; } @@ -103,11 +99,9 @@ if ($device['os_group'] == 'unix') { foreach (array_keys($agent_data['app']) as $key) { if (file_exists("includes/polling/applications/$key.inc.php")) { - if ($debug) { - echo "Enabling $key for $device['hostname'] if not yet enabled\n"; - } + d_echo("Enabling $key for ".$device['hostname']." if not yet enabled\n"); - if (in_array($key, array('apache', 'mysql', 'nginx')) { + if (in_array($key, array('apache', 'mysql', 'nginx'))) { if (dbFetchCell('SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ?', array($device['device_id'], $key)) == '0') { echo "Found new application '$key'\n"; dbInsert(array('device_id' => $device['device_id'], 'app_type' => $key), 'applications');