Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 881471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:15:55+00:00 2026-05-15T12:15:55+00:00

I have a script that polls a MySQL database (Nagios, specifically) for the date/time

  • 0

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 />";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:15:56+00:00Added an answer on May 15, 2026 at 12:15 pm
    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an R script that pulls data from a MySQL database, processes it,
I have a PHP script that pulls keywords from a MySQL database and I
I have a jQuery script that polls my server for new data, but it
I have a php page that pulls data from a mysql database based on
I have a really confusing problem. I have a PHP script that polls another
I have a script that polls the inbox every minute and then does some
I have a script that pulls data from a database, and based on the
I have a MySQL 5 database that is updated every 5 minutes from a
I have a perl script that pulls serialized php data from a database, unserializes
I have a very simple script that pulls data from a mysql db, However

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.