PHP 中按小时计算出现次数

发布于 2024-09-07 22:19:50 字数 1539 浏览 6 评论 0原文

我有一个脚本可以轮询 MySQL 数据库(特别是 Nagios)以获取事件的日期/时间。我想根据我的脚本返回的所有数据来计算每小时发生的事件数。

结果如下:(每个都在自己的行上)


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 脚本' s 输出并返回第 1 列中出现的次数以及它们在第 2 列中出现的时间。

我需要将其合并到我的 PHP 脚本中。

这是我现在从 Nagios DB 检索它的代码:

$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 />";
}

I have a script that polls a MySQL database (Nagios, specifically) for the date/time of events. I want to count how many events occur in each hour based upon all of the data my script returns.

Here is what the results look like: (each on their own line)


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

Here is the bash script that I use to get what I'm looking for:


  cat temp |awk '{print $2 $0}' | cut -d: -f1 | sort | uniq -c |sort -k2 -n

Here are some sample results:



 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

So, you can see from the above results that the little bash command parses my PHP script'
s output and returns how many occurrences in column 1 and what hour of the day they occurred at in column 2.

I need to incorporate this into my PHP script.

Here is my code right now that retrieves it from the Nagios DB:

$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 />";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪我们做朋友吧 2024-09-14 22:19:50
SELECT COUNT(*) AS `cnt`, HOUR(`state_time`) AS `hr` FROM nagios_statehistory
WHERE object_id='$tosearch' AND output='CRITICAL - Socket timeout after 10 seconds'
GROUP BY `hr`
ORDER BY state_time DESC
SELECT COUNT(*) AS `cnt`, HOUR(`state_time`) AS `hr` FROM nagios_statehistory
WHERE object_id='$tosearch' AND output='CRITICAL - Socket timeout after 10 seconds'
GROUP BY `hr`
ORDER BY state_time DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文