1<?php
2
3/**
4 * Observium
5 *
6 *   This file is part of Observium.
7 *
8 * @package    observium
9 * @subpackage poller
10 * @copyright  (C) 2006-2013 Adam Armstrong, (C) 2013-2019 Observium Limited
11 *
12 */
13
14// FIXME. Also uses check_valid_virtual_machines(). We (should) do a lot more discovery through the agent, IMO we should do away with the distinction. --tom
15include_once("includes/discovery/functions.inc.php");
16
17global $valid, $agent_sensors;
18
19if ($device['os_group'] == "unix")
20{
21  echo("Observium UNIX Agent: ");
22
23  // Use port configured in config (or defaults)
24  $agent_port = $config['unix-agent']['port'];
25
26  // ... Unless user configured a port for this specific device, and it's valid (numeric and within 16-bit port range)
27  $override_port = get_dev_attrib($device, 'agent_port');
28
29  if (is_numeric($override_port) && $override_port < 65536)
30  {
31    $agent_port = $override_port;
32  }
33
34  $agent_start = utime();
35  $agent_socket = "tcp://".$device['hostname'].":".$agent_port;
36  $agent = @stream_socket_client($agent_socket, $errno, $errstr, 10);
37
38  if (!$agent)
39  {
40    print_warning("Connection to UNIX agent on ".$agent_socket." failed. ERROR: ".$errno." ".$errstr);
41    logfile("UNIX-AGENT: Connection on ".$agent_socket." failed. ERROR: ".$errno." ".$errstr);
42  } else {
43    $agent_raw = stream_get_contents($agent);
44  }
45
46  $agent_end = utime(); $agent_time = round(($agent_end - $agent_start) * 1000);
47
48  if (!empty($agent_raw))
49  {
50    echo("execution time: ".$agent_time."ms");
51    rrdtool_update_ng($device, 'agent', array('time' => $agent_time));
52    $graphs['agent'] = TRUE;
53
54    foreach (explode("<<<", $agent_raw) as $section)
55    {
56      list($section, $data) = explode(">>>", $section);
57
58      $sa = '';
59      $sb = '';
60      $sc = '';
61
62      # DO -NOT- ADD NEW CASES BELOW -- use <<<app-$foo>>> in your application script
63      switch ($section)
64      {
65        // Compatibility with versions of scripts with and without app-
66        // Disabled for DRBD because it falsely detects the check_mk output
67        case "drbd":
68          $sa = "app"; $sb = "drbd";
69          break;
70        case "apache":
71          $sa = "app"; $sb = "apache";
72          break;
73        case "mysql":
74          $sa = "app"; $sb = "mysql";
75          break;
76        case "nginx":
77          $sa = "app"; $sb = "nginx";
78          break;
79        # FIXME why is this here? New application scripts should just return app-$foo
80        case "freeradius":
81          $sa = "app"; $sb = "freeradius";
82          break;
83        case "postfix_qshape":
84          $sa = "app"; $sb = "postfix_qshape";
85          break;
86        case "postfix_mailgraph":
87          $sa = "app"; $sb = "postfix_mailgraph";
88          break;
89        # Workaround for older script where we didn't split into 3 possible parts yet
90        case "app-powerdns-recursor":
91          $sa = "app"; $sb = "powerdns-recursor"; $sc = "";
92          break;
93        case "app-exim-mailqueue":
94          $sa = "app"; $sb = "exim-mailqueue"; $sc = "";
95          break;
96        default:
97          // default section name: "app-some-sc"
98          list($sa, $sb, $sc) = explode("-", $section, 3);
99      }
100      # DO -NOT- ADD NEW CASES ABOVE -- use <<<app-$foo>>> in your application script
101
102      if ($section != "app-drbd" && !empty($sa) && !empty($sb))
103      {
104        if (!empty($sc))
105        {
106          $agent_data[$sa][$sb][$sc] = trim($data);
107        } else {
108          $agent_data[$sa][$sb] = trim($data);
109        }
110      } else {
111        $agent_data[$section] = trim($data);
112      }
113    }
114
115    $agent_sensors = array(); # Init to empty to be able to use array_merge() later on
116
117    if (OBS_DEBUG && count($agent_data)) { print_vars($agent_data); }
118
119    include("unix-agent/packages.inc.php");
120    include("unix-agent/munin-plugins.inc.php");
121
122    foreach (array_keys($agent_data) as $key)
123    {
124      if (file_exists($config['install_dir'].'/includes/polling/unix-agent/'.$key.'.inc.php'))
125      {
126        print_debug('Including: unix-agent/'.$key.'.inc.php');
127
128        include($config['install_dir'].'/includes/polling/unix-agent/'.$key.'.inc.php');
129      } else {
130        print_warning("No include: ".$key);
131      }
132    }
133
134    if (is_array($agent_data['app']))
135    {
136      foreach (array_keys($agent_data['app']) as $key)
137      {
138        if (file_exists('includes/polling/applications/'.$key.'.inc.php'))
139        {
140          echo(" ");
141
142          // FIXME Currently only used by drbd to get $app['app_instance'] - shouldn't the drbd parser get instances from somewhere else?
143          // $app = @dbFetchRow("SELECT * FROM `applications` WHERE `device_id` = ? AND `app_type` = ?", array($device['device_id'],$key));
144
145          // print_debug('Including: applications/'.$key.'.inc.php');
146
147          // echo($key);
148
149          //include('includes/polling/applications/'.$key.'.inc.php');
150
151          $valid_applications[$key] = $key;
152
153        }
154      }
155    }
156
157    // Processes
158    // FIXME unused
159    if (!empty($agent_data['ps']))
160    {
161      echo("\nProcesses: ");
162      foreach (explode("\n", $agent_data['ps']) as $process)
163      {
164        $process = preg_replace("/\((.*),([0-9]*),([0-9]*),([0-9\.]*)\)\ (.*)/", "\\1|\\2|\\3|\\4|\\5", $process);
165        list($user, $vsz, $rss, $pcpu, $command) = explode("|", $process, 5);
166        $processlist[] = array('user' => $user, 'vsz' => $vsz, 'rss' => $rss, 'pcpu' => $pcpu, 'command' => $command);
167      }
168      #print_vars($processlist);
169      echo("\n");
170    }
171  }
172
173  echo("Sensors: ");
174  foreach (array_keys($config['sensor_types']) as $sensor_class)
175  {
176    check_valid_sensors($device, $sensor_class, $valid['sensor'], 'agent');
177  }
178  echo("\n");
179
180  echo("Virtual machines: ");
181  check_valid_virtual_machines($device, $valid['vm'], 'agent');
182  echo("\n");
183}
184
185// EOF
186