MySQLデータベース(具体的にはNagios)をポーリングしてイベントの日時を確認するスクリプトがあります。スクリプトが返すすべてのデータに基づいて、1時間に発生するイベントの数をカウントしたいと思います。
結果は次のようになります:(それぞれ独自の行に)
2010-03-01 03:20:26
2010-02-28 19:07:26
2010-02-28 00:50:37
2010-02-27 17:07:35
2010-02-27 17:06:35
探しているものを取得するために使用するbashスクリプトは次のとおりです。
cat temp |awk '{print $2 $0}' | cut -d: -f1 | sort | uniq -c |sort -k2 -n
以下にいくつかのサンプル結果を示します。
21 00
32 01
31 02
46 03
34 04
12 05
11 06
8 07
107 08
56 09
16 10
55 11
50 12
33 13
23 14
34 15
11 16
18 17
14 18
25 19
9 20
5 21
38 22
20 23
したがって、上記の結果から、小さなbashコマンドがPHPスクリプトの出力を解析し、列1で発生した回数と、列2で発生した時刻を返すことがわかります。
これをPHPスクリプトに組み込む必要があります。
NagiosDBから取得する今の私のコードは次のとおりです。
$query2= "SELECT * FROM nagios_statehistory WHERE ".
"object_id='$tosearch' AND output='CRITICAL - Socket timeout " .
"after 10 seconds' ORDER BY state_time DESC";
$result2 = mysql_query($query2) or die (mysql_error());
while($row = mysql_fetch_array($result2)) {
$outtie = substr( $row['state_time'] , 0 , 10);
if ($outtie !== $lastout) {
//bolds the first occurrence of each day
echo "<b>";
}
/* print the actual time. This is what I'm looking
* to count the occurrences of */
echo $row['state_time'];
$lastout = substr( $row['state_time'] , 0 , 10);
//disable bold and insert a line break
echo "</b><br />";
}