/** * Get Event * * @author AvB **/ function get($minutes = 60, $type = 'all', $module = 'all') { $queryobj = new Event_model(); $queryobj = new Reportdata_model(); $fromtime = time() - 60 * $minutes; $out['items'] = array(); $out['error'] = ''; $sql = "SELECT m.serial_number, module, type, msg, data, m.timestamp,\n\t\t\t\t\tmachine.computer_name\n\t\t\t\tFROM event m \n\t\t\t\tLEFT JOIN reportdata USING (serial_number) \n\t\t\t\tLEFT JOIN machine USING (serial_number) \n\t\t\t\tWHERE m.timestamp > {$fromtime} \n\t\t\t\t" . get_machine_group_filter('AND') . "\n\t\t\t\tORDER BY m.timestamp DESC"; foreach ($queryobj->query($sql) as $obj) { $out['items'][] = $obj; } $obj = new View(); $obj->view('json', array('msg' => $out)); }
/** * Hash check script for clients * * Clients check in hashes using $_POST * This script returns a JSON array with * hashes that are different * * @author AvB **/ function hash_check() { // Check if we have a serial and data if (!isset($_POST['serial'])) { $this->error("Serial is missing"); } if (!trim($_POST['serial'])) { $this->error("Serial is empty"); } if (!isset($_POST['items'])) { $this->error("Items are missing"); } $itemarr = array('error' => ''); // Try to register client and lookup hashes in db try { // Register check and group in reportdata $report = new Reportdata_model($_POST['serial']); $report->machine_group = $this->group; $report->register()->save(); $req_items = unserialize($_POST['items']); //Todo: check if array // Get stored hashes from db $hash = new Hash(); $hashes = $hash->all($_POST['serial']); // Compare sent hashes with stored hashes foreach ($req_items as $name => $val) { // All models are lowercase $lkey = strtolower($name); // Rename legacy InventoryItem to inventory $lkey = str_replace('inventoryitem', 'inventory', $lkey); // Remove _model legacy if (substr($lkey, -6) == '_model') { $lkey = substr($lkey, 0, -6); } if (!(isset($hashes[$lkey]) && $hashes[$lkey] == $val['hash'])) { $itemarr[$name] = 1; } } } catch (Exception $e) { error('hash_check: ' . $e->getMessage()); } // Handle errors foreach ($GLOBALS['alerts'] as $type => $list) { foreach ($list as $msg) { $itemarr['error'] .= "{$type}: {$msg}\n"; } } // Return list of changed hashes echo serialize($itemarr); }
/** * REST interface, returns json with ip address ranges * defined in conf('ipv4router') * or passed with GET request * * @return void * @author AvB **/ function routers() { if (!$this->authorized()) { die('Authenticate first.'); // Todo: return json? } $router_arr = array(); // See if we're being parsed a request object if (array_key_exists('req', $_GET)) { $router_arr = (array) json_decode($_GET['req']); } if (!$router_arr) { $router_arr = conf('ipv4routers', array()); } $out = array(); $reportdata = new Reportdata_model(); // Compile SQL $cnt = 0; $sel_arr = array('COUNT(1) as count'); foreach ($router_arr as $key => $value) { if (is_scalar($value)) { $value = array($value); } $when_str = ''; foreach ($value as $k => $v) { $when_str .= sprintf(" WHEN ipv4router LIKE '%s%%' THEN 1", $v); } $sel_arr[] = "SUM(CASE {$when_str} ELSE 0 END) AS r{$cnt}"; $cnt++; } $sql = "SELECT " . implode(', ', $sel_arr) . " FROM network\n\t\t\tWHERE ipv4router != '(null)' AND ipv4router != ''"; // Create Out array if ($obj = current($reportdata->query($sql))) { $cnt = $total = 0; foreach ($router_arr as $key => $value) { $col = 'r' . $cnt++; $out[] = array('key' => $key, 'cnt' => intval($obj->{$col})); $total += $obj->{$col}; } // Add Remaining IP's as other if ($obj->count - $total) { $out[] = array('key' => 'Other', 'cnt' => $obj->count - $total); } } $obj = new View(); $obj->view('json', array('msg' => $out)); }
/** * Flotr2 interface, returns json with ip address ranges * defined in conf('ip_ranges') * or passed with GET request * * @return void * @author AvB **/ function ip() { $ip_arr = array(); // See if we're being parsed a request object if (array_key_exists('req', $_GET)) { $ip_arr = (array) json_decode($_GET['req']); } if (!$ip_arr) { $ip_arr = conf('ip_ranges'); } $out = array(); $reportdata = new Reportdata_model(); // Compile SQL $cnt = 0; $sel_arr = array('COUNT(1) as count'); foreach ($ip_arr as $key => $value) { if (is_scalar($value)) { $value = array($value); } $when_str = ''; foreach ($value as $k => $v) { $when_str .= sprintf(" WHEN remote_ip LIKE '%s%%' THEN 1", $v); } $sel_arr[] = "SUM(CASE {$when_str} ELSE 0 END) AS r{$cnt}"; $cnt++; } $sql = "SELECT " . implode(', ', $sel_arr) . " FROM reportdata"; // Create Out array if ($obj = current($reportdata->query($sql))) { $cnt = $total = 0; foreach ($ip_arr as $key => $value) { $col = 'r' . $cnt++; $out[] = array('label' => $key, 'data' => array(array(0, intval($obj->{$col})))); $total += $obj->{$col}; } // Add Remaining IP's as other $out[] = array('label' => 'Other', 'data' => array(array(0, intval($obj->count - $total)))); } $obj = new View(); $obj->view('json', array('msg' => $out)); }
/** * Return Machinegroup data for groupid or all groups if groupid is empty * * @return void * @author **/ function get_mg_data($groupid = "") { $out = array(); // Get created Machine Groups $mg = new Machine_group(); foreach ($mg->all($groupid) as $arr) { $out[$arr['groupid']] = $arr; } $reportdata = new Reportdata_model(); // Get registered machine groups $reportdata = new Reportdata_model(); foreach ($reportdata->get_groups(true) as $obj) { if (!isset($out[$obj->machine_group])) { $out[$obj->machine_group] = array('groupid' => $obj->machine_group, 'name' => 'Group ' . $obj->machine_group); } $out[$obj->machine_group]['cnt'] = $obj->cnt; } $obj = new View(); $obj->view('json', array('msg' => array_values($out))); }
/** * Get machine_group * * @return integer computer group * @author AvB **/ function get_machine_group($serial_number = '') { if (!isset($GLOBALS['machine_groups'][$serial_number])) { $reportdata = new Reportdata_model(); if ($reportdata->retrieve_one('serial_number=?', $serial_number)) { $GLOBALS['machine_groups'][$serial_number] = $reportdata->machine_group; } else { $GLOBALS['machine_groups'][$serial_number] = 0; } } return $GLOBALS['machine_groups'][$serial_number]; }
<?php $queryobj = new Reportdata_model(); $now = time(); $hour_ago = $now - 3600; $today = strtotime('today'); $week_ago = $now - 3600 * 24 * 7; $month_ago = $now - 3600 * 24 * 30; $three_month_ago = $now - 3600 * 24 * 90; $sql = "SELECT COUNT(1) as total, \n\tCOUNT(CASE WHEN timestamp > {$hour_ago} THEN 1 END) AS lasthour, \n\tCOUNT(CASE WHEN timestamp > {$today} THEN 1 END) AS today, \n\tCOUNT(CASE WHEN timestamp > {$week_ago} THEN 1 END) AS lastweek,\n\tCOUNT(CASE WHEN timestamp > {$month_ago} THEN 1 END) AS lastmonth,\n\tCOUNT(CASE WHEN timestamp BETWEEN {$month_ago} AND {$week_ago} THEN 1 END) AS inactive_week,\n\tCOUNT(CASE WHEN timestamp BETWEEN {$three_month_ago} AND {$month_ago} THEN 1 END) AS inactive_month,\n\tCOUNT(CASE WHEN timestamp < {$three_month_ago} THEN 1 END) AS inactive_three_month\n\tFROM reportdata"; $obj = current($queryobj->query($sql)); ?> <div class="col-lg-4 col-md-6"> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title"><i class="fa fa-group"></i> Active clients</h3> </div> <div class="panel-body text-center"> <?php if ($obj) { ?> <a href="<?php echo url('show/listing/clients');
<div class="col-lg-4 col-md-6"> <div class="panel panel-default"> <div class="panel-heading" data-container="body"> <h3 class="panel-title"><i class="fa fa-power-off"></i> Uptime</h3> </div> <div class="panel-body text-center"> <?php //FIXME use query to count totals! $machine = new Reportdata_model(); $in_green = 0; $in_yellow = 0; $in_red = 0; $filter = get_machine_group_filter('AND'); $sql = "SELECT timestamp, uptime\n\t\t\t\t\t\t\t\t\t\tFROM reportdata\n\t\t\t\t\t\t\t\t\t\tWHERE uptime <> '0'\n\t\t\t\t\t\t\t\t\t\t{$filter}\n\t\t\t\t\t\t\t\t\t\tORDER BY timestamp DESC"; foreach ($machine->query($sql) as $obj) { if ($obj->uptime <= 86400) { $in_green += 1; } elseif ($obj->uptime >= 604800) { $in_red += 1; } else { $in_yellow += 1; } } // end foreach ?>
/** * Report broken client * * Use this method to report on client-side * errors that prevent the client to * report properly * * @author AvB **/ function broken_client() { // At least, we need a serial number if (!isset($_POST['serial'])) { $this->msg("Serial is missing", TRUE); } // Register check in reportdata $report = new Reportdata_model($_POST['serial']); $report->register()->save(); // Clean POST data $data['module'] = isset($_POST['module']) ? $_POST['module'] : 'generic'; $data['type'] = isset($_POST['type']) ? $_POST['type'] : 'danger'; $data['msg'] = isset($_POST['msg']) ? $_POST['msg'] : 'Unknown'; $data['timestamp'] = time(); // Store event store_event($_POST['serial'], $data['module'], $data['type'], $data['msg']); echo "Recorded this message: " . $data['msg'] . "\n"; }
/** * Set session properties * **/ function set_session_props($show = false) { // Initialize session $this->authorized(); // Check if we are in a session if (!isset($_SESSION['auth'])) { $msg = array('error' => 'unauthorized'); $obj = new View(); $obj->view('json', array('msg' => $msg)); return; } // Default role is user $_SESSION['role'] = 'user'; $_SESSION['role_why'] = 'Default role'; // Find role in config for current user foreach (conf('roles', array()) as $role => $members) { // Check for wildcard if (in_array('*', $members)) { $_SESSION['role'] = $role; $_SESSION['role_why'] = 'Matched on wildcard (*) in ' . $role; break; } // Check if user or group is present in members foreach ($members as $member) { if (strpos($member, '@') === 0) { // groups (start with @) if (in_array(substr($member, 1), $_SESSION['groups'])) { $_SESSION['role'] = $role; $_SESSION['role_why'] = 'member of ' . $member; break 2; } } else { // user if ($member == $_SESSION['user']) { $_SESSION['role'] = $role; $_SESSION['role_why'] = $member . ' in "' . $role . '" role array'; break 2; } } } } // Check if Business Units are enabled in the config file $bu_enabled = conf('enable_business_units', FALSE); // Check if user is global admin if ($_SESSION['auth'] == 'noauth' or $_SESSION['role'] == 'admin') { unset($_SESSION['business_unit']); } elseif (!$bu_enabled) { // Regular user w/o business units enabled unset($_SESSION['business_unit']); } elseif ($bu_enabled) { // Authorized user, not in business unit $_SESSION['role'] = 'nobody'; $_SESSION['role_why'] = 'Default role for Business Units'; $_SESSION['business_unit'] = 0; // Lookup user in business units $bu = new Business_unit(); if ($bu->retrieve_one("property IN ('manager', 'user') AND value=?", $_SESSION['user'])) { $_SESSION['role'] = $bu->property; // manager, user $_SESSION['role_why'] = $_SESSION['user'] . ' found in Business Unit ' . $bu->unitid; $_SESSION['business_unit'] = $bu->unitid; } else { // Lookup groups in Business Units foreach ($_SESSION['groups'] as $group) { if ($bu->retrieve_one("property IN ('manager', 'user') AND value=?", '@' . $group)) { $_SESSION['role'] = $bu->property; // manager, user $_SESSION['role_why'] = 'Group "' . $group . '" found in Business Unit ' . $bu->unitid; $_SESSION['business_unit'] = $bu->unitid; break; } } } } // Set machine_groups if ($_SESSION['role'] == 'admin' or !$bu_enabled) { // Can access all defined groups (from machine_group) // and used groups (from reportdata) $mg = new Machine_group(); $report = new Reportdata_model(); $_SESSION['machine_groups'] = array_unique(array_merge($report->get_groups(), $mg->get_group_ids())); } else { // Only get machine_groups for business unit $_SESSION['machine_groups'] = $bu->get_machine_groups($bu->unitid); } // Show current session info if ($show) { $obj = new View(); $obj->view('json', array('msg' => $_SESSION)); } }