function get_device_groups() { $app = \Slim\Slim::getInstance(); $router = $app->router()->getCurrentRoute()->getParams(); $status = 'error'; $code = 404; $hostname = $router['hostname']; // use hostname as device_id if it's all digits $device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); if (is_numeric($device_id)) { $groups = GetGroupsFromDevice($device_id, 1); } else { $groups = GetDeviceGroups(); } if (empty($groups)) { $message = 'No device groups found'; } else { $status = 'ok'; $code = 200; $message = 'Found ' . count($groups) . ' device groups'; } $output = array('status' => $status, 'message' => $message, 'count' => count($groups), 'groups' => $groups); $app->response->setStatus($code); $app->response->headers->set('Content-Type', 'application/json'); echo _json_encode($output); }
/** * Update device-group relationship for the given device id * @param $device_id */ function UpdateGroupsForDevice($device_id) { global $debug; $debug = true; $queried_groups = QueryGroupsFromDevice($device_id); $db_groups = GetGroupsFromDevice($device_id); // compare the arrays to get the added and removed groups $added_groups = array_diff($queried_groups, $db_groups); $removed_groups = array_diff($db_groups, $queried_groups); // insert new groups $insert = array(); foreach ($added_groups as $group_id) { $insert[] = array('device_id' => $device_id, 'device_group_id' => $group_id); } if (!empty($insert)) { dbBulkInsert($insert, 'device_group_device'); } // remove old groups if (!empty($removed_groups)) { dbDelete('device_group_device', '`device_id`=? AND `device_group_id` IN (?)', array($device_id, array(implode(',', $removed_groups)))); } }
/** * Check if device is under maintenance * @param int $device Device-ID * @return int */ function IsMaintenance($device) { $groups = GetGroupsFromDevice($device); $params = array($device); $where = ""; foreach ($groups as $group) { $where .= " || alert_schedule_items.target = ?"; $params[] = 'g' . $group; } return dbFetchCell('SELECT alert_schedule.schedule_id FROM alert_schedule LEFT JOIN alert_schedule_items ON alert_schedule.schedule_id=alert_schedule_items.schedule_id WHERE ( alert_schedule_items.target = ?' . $where . ' ) && NOW() BETWEEN alert_schedule.start AND alert_schedule.end LIMIT 1', $params); }
/** * Update device-group relationship for the given device id * @param $device_id */ function UpdateGroupsForDevice($device_id) { d_echo("### Start Device Groups ###\n"); $queried_groups = QueryGroupsFromDevice($device_id); $db_groups = GetGroupsFromDevice($device_id); // compare the arrays to get the added and removed groups $added_groups = array_diff($queried_groups, $db_groups); $removed_groups = array_diff($db_groups, $queried_groups); d_echo("Groups Added: " . implode(',', $added_groups) . PHP_EOL); d_echo("Groups Removed: " . implode(',', $removed_groups) . PHP_EOL); // insert new groups $insert = array(); foreach ($added_groups as $group_id) { $insert[] = array('device_id' => $device_id, 'device_group_id' => $group_id); } if (!empty($insert)) { dbBulkInsert($insert, 'device_group_device'); } // remove old groups if (!empty($removed_groups)) { dbDelete('device_group_device', '`device_id`=? AND `device_group_id` IN (?)', array($device_id, array(implode(',', $removed_groups)))); } d_echo("### End Device Groups ###\n"); }