Skip to content

Instantly share code, notes, and snippets.

@mvance
Last active January 4, 2016 23:59
Show Gist options
  • Save mvance/3cde0bea57b2af0c57d9 to your computer and use it in GitHub Desktop.
Save mvance/3cde0bea57b2af0c57d9 to your computer and use it in GitHub Desktop.
Find long-running tasks in Ansible logs by prepending elapsed time for each entry in a log file
<?php
// This script prepends the elapsed time for each entry of an Ansible log
// file (see: http://stackoverflow.com/q/34211898/147937 for inspiration and
// additional background information). This script is intended to be used
// only on the "TASK:" lines from the logs. For example:
// $ grep "TASK:" ansible.log | php ~/tansible.php
while(!feof(STDIN)) {
$line = fgets(STDIN);
$exploded_line = explode(' ', $line);
if (isset($exploded_line[1])) {
$exploded_time = explode(',', $exploded_line[1]);
}
else {
exit();
}
$hms = $exploded_time[0];
if (isset($previous_line)) {
$difference = strtotime($hms) - strtotime($previous_time);
if ($difference > 60) {
echo(gmdate("H:i:s", $difference) . ' ' . $previous_line . PHP_EOL);
}
}
$previous_line = $line;
$previous_time = $hms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment