function renderTopNPrettyLine($input_array, $limit = 4) { if (!is_array($input_array)) { return false; } $ROOT_URL = getTeamConfig('root_url'); arsort($input_array); $a = array_splice($input_array, 0, $limit); foreach ($a as $k => $v) { $strings[] = "<strong>{$k}</strong> ({$v} times)"; } return implode($strings, ", "); }
static function connect() { global $mysql_host, $mysql_user, $mysql_pass; $mysql_db = getTeamConfig('database'); if (function_exists("mysqli_connect")) { $mysqli = new mysqli($mysql_host, $mysql_user, $mysql_pass, $mysql_db); if ($mysqli->connect_error) { echo insertNotify("critical", $mysqli->connect_error); return false; } self::$dbh = $mysqli; } else { if (function_exists("mysql_connect")) { if (!($mysql = mysql_connect($mysql_host, $mysql_user, $mysql_pass))) { echo insertNotify("critical", mysql_error()); return false; } if (!mysql_select_db($mysql_db, $mysql)) { echo insertNotify("critical", mysql_error()); return false; } } else { echo "MySQL support is required"; return false; } } return true; }
<script> $('.textarea').wysihtml5({"image": false, "color": false}); </script> <input type="hidden" name="range_start" value="<?php echo $start_ts; ?> "> <input type="hidden" name="range_end" value="<?php echo $end_ts; ?> "> <button class="btn btn-primary" type="submit">Save Meeting Notes</button> </form> <?php if (getTeamConfig('oncall')) { ?> <h2>On Call Report <?php if ($results = getOnCallReportForWeek($oncall_start, $oncall_end)) { echo "<small> for week " . date("l jS F Y", $oncall_start) . " - " . date("l jS F Y", $oncall_end); echo " compiled by " . guessPersonOnCall($oncall_start, $oncall_end) . "</small></h2>"; ?> <table class="table table-striped table-bordered table-hover" id="oncall-table" style='font-size: 90%'> <thead> <tr> <th>Date/Time</th><th>Host</th><th>Service</th><th>Output</th><th>State</th> </tr> </thead> <tbody>
// See if this user is enrolled in sleep tracking $profile_data = checkForUserProfile($username); if ($profile_data && $profile_data['sleeptracking_provider'] != "none" && $profile_data['sleeptracking_provider'] != "") { $sleep_provider = $profile_data['sleeptracking_provider']; logline("Sleeptracking enabled: {$sleep_provider}"); $sleep = true; // Get the user settings into an array by decoding the JSON we put in the database for them $sleeptracking_settings = json_decode($profile_data['sleeptracking_settings'], 1); // Get the provider settings from the config array $sleepprovider_settings = $sleep_providers[$sleep_provider]; // Load the sleeptracking provider's PHP code include_once $sleepprovider_settings['lib']; } else { logline("No need to do sleep tracking because {$username} has no provider chosen"); } $event_versioning = getTeamConfig('event_versioning'); // We'll create an array of value strings for both INSERT and UPDATE // statements that will be submitted after iterating over all the // notifications we've found. For notifications found to already exist // in the database, we'll perform UPDATEs. Otherwise we'll INSERT a new // row. $insert_values = array(); $update_values = array(); foreach ($_POST['oncall']['notifications'] as $id => $n) { $sleep_state = -1; $mtts = -1; $sleep_level = -1; $confidence = -1; $timestamp = db::escape($n['time']); $hostname = db::escape($n['hostname']); $output = db::escape($n['output']);
function printOnCallNotifications($on_call_name, $start, $end, $oncall_start, $oncall_end) { global $nagios_state_to_badge, $nagios_alert_tags; # Non timezone version to store the data $range_start = $start; $range_end = $end; // Call the correct provider $oncall_config = getTeamConfig('oncall'); if (!$oncall_config) { return '<div class="alert alert-error">Something terrible has happened. This team doesn\'t have on call enabled!</div>'; } $provider_name = $oncall_config['provider']; logline("printOnCallNotifications has started, looking for provider {$provider_name}. "); // Get the global configuration for the provider $provider_config = getOnCallProvider($provider_name); $provider_global_options = $provider_config['options']; // Get the per team options for the provider $provider_team_options = getTeamOnCallConfig('provider_options'); // Hopefully the user entered a valid oncall provider name... if (!$provider_config) { return '<div class="alert alert-error">Something terrible has happened. We cannot find an on-call provider named "' . $provider_name . '"</div>'; } logline("printOnCallNotifications is loading {$provider_config['lib']}"); // Load the provider specified. include_once "{$provider_config['lib']}"; logline("Firing getOnCallNotifications..."); // And now ask for the notifications $notifications = getOnCallNotifications($on_call_name, $provider_global_options, $provider_team_options, $oncall_start, $oncall_end); // If the returned data wasn't an array, something is wrong... if (!is_array($notifications)) { logline("getOnCallNotifications didn't return an array! Returned data was {$notifications} "); return '<div class="alert alert-error">The oncall provider failed. It returned: <pre>' . $notifications . '</pre></div>'; } else { logline("getOnCallNotifications returned an array containing " . count($notifications) . " notifications"); } // Data collection complete. Time to render the form items for report submission. // First, we populate the field with the on call ranges so the report is saved with the correct timestamp $html = "<input type='hidden' name='oncall[range_start]' value='{$range_start}'>"; $html .= "<input type='hidden' name='oncall[range_end]' value='{$range_end}'>"; $timezone = getTimezoneSetting(); date_default_timezone_set($timezone); $n_num = 0; $n_total = count($notifications); $timesaver = false; foreach ($notifications as $n) { # Add a row that lets the user potentially stop halfway and come back later if ($n_num >= $n_total / 2 && !$timesaver) { $timesaver = true; $html .= "<tr><td colspan='5'><div class='well'><b>Hey!</b> You made it halfway. If you want you can save up to here and continue later."; $html .= "<button class='btn btn-primary pull-right' type='submit'>Save draft</button></div></td></tr>"; } # Look for a previous copy of this alert if ($previous_data = checkForPreviousOnCallItem(generateOnCallAlertID($n['time'], $n['hostname'], $n['service']))) { $previous_notes = $previous_data['notes']; $previous_tag = $previous_data['tag']; } else { $previous_notes = null; $previous_tag = null; } $pretty_date = date("D d M H:i:s T", $n['time']); $html .= "<tr>"; $html .= "<td>{$pretty_date}</td><td>{$n['hostname']}</td><td>{$n['service']}</td><td><pre><small>{$n['output']}</small></pre></td>"; $html .= "<td><span class='label label-{$nagios_state_to_badge[$n['state']]}'>{$n['state']}</span></td>"; # Need to populate all the information into hidden fields so we get all the data back nicely when the form is submitted $html .= "<input type='hidden' name='oncall[notifications][not_{$n_num}][hostname]' value='{$n['hostname']}'>"; $html .= "<input type='hidden' name='oncall[notifications][not_{$n_num}][output]' value='{$n['output']}'>"; $html .= "<input type='hidden' name='oncall[notifications][not_{$n_num}][time]' value='{$n['time']}'>"; $html .= "<input type='hidden' name='oncall[notifications][not_{$n_num}][state]' value='{$n['state']}'>"; $html .= "<input type='hidden' name='oncall[notifications][not_{$n_num}][service]' value='{$n['service']}'>"; $html .= "</tr>"; $html .= "<tr><td colspan='2'>"; # Dropdown that lets the user choose a tag for the alert $html .= "<select name='oncall[notifications][not_{$n_num}][tag]' class='input-xlarge'>"; foreach ($nagios_alert_tags as $tag => $tag_name) { $selected = $tag == $previous_tag ? " selected" : ""; $html .= "<option value='{$tag}'{$selected}>{$tag_name}</option>"; } $html .= "</select></td>"; $html .= "<td colspan='2'><div class='control-group'><label class='control-label'><b>Notes:</b> </label>\n <div class='controls'><input type='text' name='oncall[notifications][not_{$n_num}][notes]' class='input-xxlarge' placeholder='Notes' value='{$previous_notes}'></div></div></td>"; $html .= "<td><input class='bulk-check' data-num='{$n_num}' type='checkbox'></td></tr>"; $n_num++; } date_default_timezone_set("UTC"); return $html; }
} } } $all_wake_ups_pct = $all_sleep_status_agg[1] / ($all_sleep_status_agg[1] + $all_sleep_status_agg[0]) * 100; $total_per_week = round($total_notifications / $total_weeks, 2); $all_mtts = round($all_mtts_total / $all_sleep_status_agg[1] / 60, 2); $all_noaction_pct = $all_tag_agg_norm['noaction'] / ($all_tag_agg_norm['noaction'] + $all_tag_agg_norm['action']) * 100; $per_week_diff = $total_per_week - $personal_per_week; $per_week_diff = $total_per_week > $personal_per_week ? "<span class='text-success'><i class='icon-arrow-up'></i> {$per_week_diff} better off</span>" : "<span class='text-error'><i class='icon-arrow-down'></i> {$per_week_diff} worse off</span>"; if ($profile && $profile['sleeptracking_provider'] != "none" && $profile['sleeptracking_provider'] != "") { $sleep_tracking = true; $sleep_name = $sleep_providers[$profile['sleeptracking_provider']]['display_name']; $sleep_logo = $sleep_providers[$profile['sleeptracking_provider']]['logo']; } // Build up sleep-related metrics for the last few on-call rotations, for a simple retrospective. $oncall_sleep_retrospective_count = getTeamConfig('oncall_sleep_retrospective_count'); if ($oncall_sleep_retrospective_count) { // Get the last few on-call rotations, in descending order. $oncall_sleep_retrospective_weeks = array_slice(array_reverse($oncall_ranges), 0, $oncall_sleep_retrospective_count); foreach ($oncall_sleep_retrospective_weeks as $week) { $results = getOnCallReportForWeek($week['range_start'], $week['range_end']); $week_wake_ups = 0; $week_sleep_loss = 0; // Seconds. $week_sleep_abandoned = 0; foreach ($results as $n) { // Count the number of wake ups. if ($n['sleep_state'] > 0) { $week_wake_ups++; } // If we have non-zero MTTS data and the operator was asleep (1), sum the sleep loss.
function printOnCallNotifications($on_call_name, $start, $end, $oncall_start, $oncall_end) { global $nagios_state_to_badge, $nagios_alert_tags; # Non timezone version to store the data $range_start = $start; $range_end = $end; // Call the correct provider $oncall_config = getTeamConfig('oncall'); if (!$oncall_config) { return '<div class="alert alert-error">Something terrible has happened. This team doesn\'t have on call enabled!</div>'; } $provider_name = $oncall_config['provider']; logline("printOnCallNotifications has started, looking for provider {$provider_name}. "); // Get the global configuration for the provider $provider_config = getOnCallProvider($provider_name); $provider_global_options = $provider_config['options']; // Get the per team options for the provider $provider_team_options = getTeamOnCallConfig('provider_options'); // Hopefully the user entered a valid oncall provider name... if (!$provider_config) { return '<div class="alert alert-error">Something terrible has happened. We cannot find an on-call provider named "' . $provider_name . '"</div>'; } logline("printOnCallNotifications is loading {$provider_config['lib']}"); // Load the provider specified. include_once "{$provider_config['lib']}"; logline("Firing getOnCallNotificationsFromDb to get events stored in the database for the period..."); $notifications_from_db = getOnCallNotificationsFromDb($oncall_start, $oncall_end); if (empty($notifications_from_db)) { logline("getOnCallNotificationsFromDb returned an empty array! The oncall provider may have events..."); } else { logline("getOnCallNotificationsFromDb returned an array containing " . count($notifications_from_db) . " notifications"); // Find the most recent event in the database and return its timestamp. // We'll use this as the start timestamp for querying the oncall provider. $most_recent_db_event_timestamp = getMostRecentEventTimestampFromDb($oncall_start, $oncall_end); logline("Most recent event timestamp from the database: {$most_recent_db_event_timestamp}"); $oncall_start = $most_recent_db_event_timestamp; } logline("Firing getOnCallNotifications..."); // And now ask for the notifications $notifications_from_provider = getOnCallNotifications($on_call_name, $provider_global_options, $provider_team_options, $oncall_start, $oncall_end); if (empty($notifications_from_provider)) { logline("getOnCallNotifications returned an empty array! Continuing with the events stored in the database."); $notifications = $notifications_from_db; } else { logline("getOnCallNotifications returned an array containing " . count($notifications) . " notifications"); if (empty($notifications_from_db)) { // This is probably the first time the on-call engineer started writing the report so nothing is stored in the database. $notifications = $notifications_from_provider; } else { // Merge the results from the database into what we've received from our oncall provider. logline("Merging the database results with the provider results."); $notifications = array_merge($notifications_from_provider, $notifications_from_db); } } // Data collection complete. Time to render the form items for report submission. // First, we populate the field with the on call ranges so the report is saved with the correct timestamp $html = "<input type='hidden' name='oncall[range_start]' value='{$range_start}'>"; $html .= "<input type='hidden' name='oncall[range_end]' value='{$range_end}'>"; // Add some javascript so we can toggle hidden events. $html .= "<script type='text/javascript'>"; $html .= " function toggleHiddenEvents() {"; $html .= " var events = document.getElementsByClassName('hiddenEvent');"; $html .= " for (var i=0; i < events.length; i++) {"; $html .= " events[i].style.display = events[i].style.display == 'none' ? '' : 'none';"; $html .= " }"; $html .= " }"; $html .= "</script>"; // Allow the user to display/hide hidden events. $html .= "<button class='btn btn-primary pull-right' type='button' onclick='toggleHiddenEvents()'>Toggle Hidden Events</button><br><br>"; $timezone = getTimezoneSetting(); date_default_timezone_set($timezone); $n_num = 0; $n_total = count($notifications); $timesaver = false; if ($n_total > 1) { foreach ($notifications as $n) { # Add a row that lets the user potentially stop halfway and come back later if ($n_num >= $n_total / 2 && !$timesaver) { $timesaver = true; $html .= "<tr><td colspan='7'><div class='well'><b>Hey!</b> You made it halfway. If you want you can save up to here and continue later."; $html .= "<button class='btn btn-primary pull-right' type='submit'>Save draft</button></div></td></tr>"; } // Determine if we need to display a checked box for a hidden event. $hide_checked = $n['hide_event'] ? "checked" : ""; $pretty_date = date("D d M H:i:s T", $n['time']); if ($n['hide_event']) { $html .= "<tr class='hiddenEvent' style='display:none'>"; } else { $html .= "<tr>"; } $html .= "<td>{$pretty_date}</td><td>{$n['hostname']}</td><td>{$n['service']}</td><td><pre><small>{$n['output']}</small></pre></td>"; $html .= "<td><span class='label label-{$nagios_state_to_badge[$n['state']]}'>{$n['state']}</span></td>"; # Need to populate all the information into hidden fields so we get all the data back nicely when the form is submitted $html .= "<input type='hidden' name='oncall[notifications][not_{$n_num}][hostname]' value='{$n['hostname']}'>"; $html .= "<input type='hidden' name='oncall[notifications][not_{$n_num}][output]' value='{$n['output']}'>"; $html .= "<input type='hidden' name='oncall[notifications][not_{$n_num}][time]' value='{$n['time']}'>"; $html .= "<input type='hidden' name='oncall[notifications][not_{$n_num}][state]' value='{$n['state']}'>"; $html .= "<input type='hidden' name='oncall[notifications][not_{$n_num}][service]' value='{$n['service']}'>"; if ($n['id']) { $html .= "<input type='hidden' name='oncall[notifications][not_{$n_num}][id]' value='{$n['id']}'>"; } $html .= "<td><input class='bulk-check' data-num='{$n_num}' type='checkbox'></td>"; $html .= "<td><input class='hide-check' data-num='{$n_num}' name='oncall[notifications][not_{$n_num}][hide_event]' type='checkbox' {$hide_checked}></td>"; $html .= "</tr>"; if ($n['hide_event']) { $html .= "<tr class='hiddenEvent' style='display:none'>"; } else { $html .= "<tr>"; } $html .= "<td colspan='2'>"; # Dropdown that lets the user choose a tag for the alert $html .= "<select name='oncall[notifications][not_{$n_num}][tag]' class='input-xlarge'>"; foreach ($nagios_alert_tags as $tag => $tag_name) { //$selected = ($tag == $previous_tag) ? " selected" : ""; $selected = $tag == $n['tag'] ? " selected" : ""; $html .= "<option value='{$tag}'{$selected}>{$tag_name}</option>"; } $html .= "</select></td>"; $html .= "<td colspan='5'><div class='control-group'><label class='control-label'><b>Notes:</b> </label>\n <div class='controls'><input type='text' name='oncall[notifications][not_{$n_num}][notes]' class='input-xxlarge' placeholder='Notes' value='{$n['notes']}'></div></div></td>"; $html .= "</tr>"; $n_num++; } } date_default_timezone_set("UTC"); return $html; }
function sendEmailReport($from_username, $report, $range_start, $range_end) { global $email_from_domain; if (!($email_report_to = getTeamConfig('email_report_to'))) { return false; } // Remove any bare linefeeds, Evernote loveeees to insert them. $report = str_replace('\\r\\n', '', $report); $subject = "Weekly Update"; $message = "<html><body>"; $message .= "<h3>Weekly report for week ending " . date("l jS F Y", $range_end) . "</h3>"; $message .= "<p>"; $message .= stripslashes($report); $message .= "</p>"; $message .= "</body></html>"; if (sendEmail($email_report_to, strip_tags("{$from_username}@{$email_from_domain}"), $subject, $message)) { return true; } else { return false; } }