/usr/share/zabbix/api/classes/class.cgraphitem.php is in zabbix-frontend-php 1:1.8.11-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | <?php
/*
** ZABBIX
** Copyright (C) 2000-2010 SIA Zabbix
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/
?>
<?php
/**
* File containing CGraphItem class for API.
* @package API
*/
/**
* Class containing methods for operations with GraphItems
*/
class CGraphItem extends CZBXAPI{
/**
* Get GraphItems data
*
* @static
* @param array $options
* @return array|boolean
*/
public static function get($options = array()){
global $USER_DETAILS;
$result = array();
$user_type = $USER_DETAILS['type'];
$userid = $USER_DETAILS['userid'];
$result = array();
$sort_columns = array('gitemid'); // allowed columns for sorting
$subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND); // allowed output options for [ select_* ] params
$sql_parts = array(
'select' => array('gitems' => 'gi.gitemid'),
'from' => array('graphs_items' => 'graphs_items gi'),
'where' => array(),
'order' => array(),
'limit' => null,
);
$def_options = array(
'nodeids' => null,
'graphids' => null,
'itemids' => null,
'type' => null,
'editable' => null,
'nopermissions' => null,
// output
'select_graphs' => null,
'output' => API_OUTPUT_REFER,
'expandData' => null,
'extendoutput' => null,
'countOutput' => null,
'preservekeys' => null,
'sortfield' => '',
'sortorder' => '',
'limit' => null
);
$options = zbx_array_merge($def_options, $options);
if(!is_null($options['extendoutput'])){
$options['output'] = API_OUTPUT_EXTEND;
}
// editable + PERMISSION CHECK
if((USER_TYPE_SUPER_ADMIN == $user_type) || $options['nopermissions']){
}
else{
$permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
$sql_parts['from']['items'] = 'items i';
$sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
$sql_parts['from']['rights'] = 'rights r';
$sql_parts['from']['users_groups'] = 'users_groups ug';
$sql_parts['where']['igi'] = 'i.itemid=gi.itemid';
$sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
$sql_parts['where'][] = 'r.id=hg.groupid ';
$sql_parts['where'][] = 'r.groupid=ug.usrgrpid';
$sql_parts['where'][] = 'ug.userid='.$userid;
$sql_parts['where'][] = 'r.permission>='.$permission;
$sql_parts['where'][] = 'NOT EXISTS( '.
' SELECT hgg.groupid '.
' FROM hosts_groups hgg, rights rr, users_groups ugg '.
' WHERE i.hostid=hgg.hostid '.
' AND rr.id=hgg.groupid '.
' AND rr.groupid=ugg.usrgrpid '.
' AND ugg.userid='.$userid.
' AND rr.permission<'.$permission.')';
}
// nodeids
$nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
// graphids
if(!is_null($options['graphids'])){
zbx_value2array($options['graphids']);
if($options['output'] != API_OUTPUT_SHORTEN){
$sql_parts['select']['graphid'] = 'gi.graphid';
}
$sql_parts['from']['graphs'] = 'graphs g';
$sql_parts['where']['gig'] = 'gi.graphid=g.graphid';
$sql_parts['where'][] = DBcondition('g.graphid', $options['graphids']);
}
// itemids
if(!is_null($options['itemids'])){
zbx_value2array($options['itemids']);
if($options['output'] != API_OUTPUT_SHORTEN){
$sql_parts['select']['itemid'] = 'gi.itemid';
}
$sql_parts['where'][] = DBcondition('gi.itemid', $options['itemids']);
}
// type
if(!is_null($options['type'] )){
$sql_parts['where'][] = 'gi.type='.$options['type'];
}
// output
if($options['output'] == API_OUTPUT_EXTEND){
$sql_parts['select']['gitems'] = 'gi.*';
}
// expandData
if(!is_null($options['expandData'])){
$sql_parts['select']['key'] = 'i.key_';
$sql_parts['select']['hostid'] = 'i.hostid';
$sql_parts['select']['host'] = 'h.host';
$sql_parts['from']['items'] = 'items i';
$sql_parts['from']['hosts'] = 'hosts h';
$sql_parts['where']['gii'] = 'gi.itemid=i.itemid';
$sql_parts['where']['hi'] = 'h.hostid=i.hostid';
}
// countOutput
if(!is_null($options['countOutput'])){
$options['sortfield'] = '';
$sql_parts['select'] = array('count(DISTINCT gi.gitemid) as rowscount');
}
// order
// restrict not allowed columns for sorting
$options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
if(!zbx_empty($options['sortfield'])){
$sortorder = ($options['sortorder'] == ZBX_SORT_DOWN)?ZBX_SORT_DOWN:ZBX_SORT_UP;
$sql_parts['order'][] = 'gi.'.$options['sortfield'].' '.$sortorder;
if(!str_in_array('gi.'.$options['sortfield'], $sql_parts['select']) && !str_in_array('gi.*', $sql_parts['select'])){
$sql_parts['select'][] = 'gi.'.$options['sortfield'];
}
}
// limit
if(zbx_ctype_digit($options['limit']) && $options['limit']){
$sql_parts['limit'] = $options['limit'];
}
//------------
$gitemids = array();
$sql_parts['select'] = array_unique($sql_parts['select']);
$sql_parts['from'] = array_unique($sql_parts['from']);
$sql_parts['where'] = array_unique($sql_parts['where']);
$sql_parts['order'] = array_unique($sql_parts['order']);
$sql_select = '';
$sql_from = '';
$sql_where = '';
$sql_order = '';
if(!empty($sql_parts['select'])) $sql_select.= implode(',',$sql_parts['select']);
if(!empty($sql_parts['from'])) $sql_from.= implode(',',$sql_parts['from']);
if(!empty($sql_parts['where'])) $sql_where.= ' AND '.implode(' AND ',$sql_parts['where']);
if(!empty($sql_parts['order'])) $sql_order.= ' ORDER BY '.implode(',',$sql_parts['order']);
$sql_limit = $sql_parts['limit'];
$sql = 'SELECT '.zbx_db_distinct($sql_parts).' '.$sql_select.
' FROM '.$sql_from.
' WHERE '.DBin_node('gi.gitemid', $nodeids).
$sql_where.
$sql_order;
//SDI($sql);
$db_res = DBselect($sql, $sql_limit);
while($gitem = DBfetch($db_res)){
if(!is_null($options['countOutput'])){
$result = $gitem['rowscount'];
}
else{
$gitemids[$gitem['gitemid']] = $gitem['gitemid'];
if($options['output'] == API_OUTPUT_SHORTEN){
$result[$gitem['gitemid']] = array('gitemid' => $gitem['gitemid']);
}
else{
if(!isset($result[$gitem['gitemid']]))
$result[$gitem['gitemid']]= array();
// graphids
if(isset($gitem['graphid']) && is_null($options['select_graphs'])){
if(!isset($result[$gitem['gitemid']]['graphs'])) $result[$gitem['gitemid']]['graphs'] = array();
$result[$gitem['gitemid']]['graphs'][] = array('graphid' => $gitem['graphid']);
// unset($gitem['graphid']);
}
$result[$gitem['gitemid']] += $gitem;
}
}
}
if(!is_null($options['countOutput'])){
if(is_null($options['preservekeys'])) $result = zbx_cleanHashes($result);
return $result;
}
// Adding graphs
if(!is_null($options['select_graphs']) && str_in_array($options['select_graphs'], $subselects_allowed_outputs)){
$obj_params = array(
'nodeids' => $nodeids,
'output' => $options['select_graphs'],
'gitemids' => $gitemids,
'preservekeys' => 1
);
$graphs = CGraph::get($obj_params);
foreach($graphs as $graphid => $graph){
$gitems = $graph['gitems'];
unset($graph['gitems']);
foreach($gitems as $inum => $item){
$result[$gitem['gitemid']]['graphs'][] = $graph;
}
}
}
// removing keys (hash -> array)
if(is_null($options['preservekeys'])){
$result = zbx_cleanHashes($result);
}
return $result;
}
/**
* Get graph items by graph id and graph item id
*
* @static
* @param _array $gitem_data
* @param array $gitem_data['itemid']
* @param array $gitem_data['graphid']
* @return string|boolean graphid
*/
public static function getObjects($gitem_data){
$result = array();
$gitemids = array();
$sql = 'SELECT gi.gitemid '.
' FROM graphs_items gi '.
' WHERE gi.itemid='.$gitem_data['itemid'].
' AND gi.graphid='.$gitem_data['graphid'].
$db_res = DBselect($sql);
while($gitem = DBfetch($db_res)){
$gitemids[$gitem['gitemid']] = $gitem['gitemid'];
}
if(!empty($gitemids))
$result = self::get(array('gitemids'=>$gitemids, 'extendoutput'=>1));
return $result;
}
}
?>
|