histogram: Make histograms crash less
[ninja.git] / application / helpers / nacoma.php
blob7c1c783ca6537d95be634236cb49e54628380fca
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Helper class for nacoma
5 * Copyright 2009 op5 AB
6 * op5, and the op5 logo are trademarks, servicemarks, registered servicemarks
7 * or registered trademarks of op5 AB.
8 * All other trademarks, servicemarks, registered trademarks, and registered
9 * servicemarks mentioned herein may be the property of their respective owner(s).
10 * The information contained herein is provided AS IS with NO WARRANTY OF ANY
11 * KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY, AND FITNESS FOR A
12 * PARTICULAR PURPOSE.
14 class nacoma_Core {
16 /**
17 * Check if a link to Nacoma should be displayed
18 * This depends on if Nacoma is actually available
19 * and if the user is authorized to use it.
21 public static function link($path=false, $img=false, $title=false)
23 # don't try this if user isn't logged in
24 if (!Auth::instance()->logged_in()) {
25 return null;
27 if (!Auth::instance()->authorized_for('configuration_information') || Kohana::config('config.nacoma_path')===false) {
28 return false;
30 # create the link.
31 $link = false;
32 if (!empty($path) && !empty($img)) {
33 $link = html::anchor($path, html::image(ninja::add_path($img),array('alt' => $title, 'title' => $title)), array('style' => 'border: 0px'));
34 } else {
35 # helper only used to decide if the link should be displayed at all
36 $link = true;
38 return $link;
41 /**
42 * Check if the current user is allowed to use Nacoma
44 * @return true/false
46 public static function allowed() {
47 if (!Auth::instance()->logged_in()) {
48 return null;
51 if (!Auth::instance()->authorized_for('configuration_information') || Kohana::config('config.nacoma_path')===false) {
52 return false;
55 return true;
58 /**
59 * Delete host (and associated services) using monitor CLI api
61 * @param $host string host to be deleted
63 public static function delHost ($host) {
64 if (!nacoma::allowed())
65 return false;
67 exec('php /opt/monitor/op5/nacoma/api/monitor.php -u ' . Auth::instance()->get_user()->username . ' -t host -n ' . escapeshellarg($host) . ' -a delete', $out, $retval);
68 var_dump($out);
69 return $retval === 0;
72 /**
73 * Given a service name, returns the name of the hostgroup this service
74 * belongs to, or false if it is a host service.
76 public static function getHostgroupForService($service) {
77 if (!nacoma::allowed())
78 return false;
80 exec('php /opt/monitor/op5/nacoma/api/monitor.php -u ' . Auth::instance()->get_user()->username . ' -t service -a show_object -n ' . escapeshellarg($service), $out, $retval);
81 if ($retval !== 0)
82 return false;
83 foreach ($out as $line) {
84 list($key, $val) = explode("=", $line);
85 if ($key == 'hostgroup_name')
86 return $val;
88 return false;
91 /**
92 * Delete the service using monitor CLI api
94 * @param $service string service to be deleted, format HOST;SERVICE
96 public static function delService ($service) {
97 if (!nacoma::allowed())
98 return false;
100 exec('php /opt/monitor/op5/nacoma/api/monitor.php -u ' . Auth::instance()->get_user()->username . ' -t service -n ' . escapeshellarg($service) . ' -a delete', $out, $retval);
101 var_dump($out);
102 return $retval === 0;