1<?php
2/**
3 * output.php
4 *
5 * runs the requested command and outputs as a file or json
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 *
20 * @link       https://www.librenms.org
21 * @copyright  2016 Tony Murray
22 * @author     Tony Murray <murraytony@gmail.com>
23 */
24if (! Auth::user()->hasGlobalAdmin()) {
25    echo 'Insufficient Privileges';
26    exit();
27}
28
29$hostname = escapeshellcmd($_REQUEST['hostname']);
30$type = $_REQUEST['type'];
31
32switch ($type) {
33    case 'poller':
34        $cmd = ['/usr/local/bin/php', \LibreNMS\Config::get('install_dir') . '/poller.php', '-h', $hostname, '-r', '-f', '-d'];
35        $filename = "poller-$hostname.txt";
36        break;
37    case 'snmpwalk':
38        $device = device_by_name($hostname);
39
40        $cmd = gen_snmpwalk_cmd($device, '.', '-OUneb');
41
42        $filename = $device['os'] . '-' . $device['hostname'] . '.snmpwalk';
43        break;
44    case 'discovery':
45        $cmd = ['/usr/local/bin/php', \LibreNMS\Config::get('install_dir') . '/discovery.php', '-h', $hostname, '-d'];
46        $filename = "discovery-$hostname.txt";
47        break;
48    default:
49        echo 'You must specify a valid type';
50        exit;
51}
52
53// ---- Output ----
54$proc = new \Symfony\Component\Process\Process($cmd);
55$proc->setTimeout(Config::get('snmp.exec_timeout', 1200));
56
57if ($_GET['format'] == 'text') {
58    header('Content-type: text/plain');
59    header('X-Accel-Buffering: no');
60
61    $proc->run(function ($type, $buffer) {
62        echo preg_replace('/\033\[[\d;]+m/', '', $buffer) . PHP_EOL;
63        ob_flush();
64        flush(); // you have to flush buffer
65    });
66} elseif ($_GET['format'] == 'download') {
67    $proc->run();
68    $output = $proc->getOutput();
69
70    $output = preg_replace('/\033\[[\d;]+m/', '', $output);
71
72    file_download($filename, $output);
73}
74