Thread: Count values

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Count values


    I am trying to scrape all logs for a value 'Error' instead of displaying 100 lines of the same error it would be better to display it once and the number of times it occurs, this way the web browser will render quicker, any ideas how I can do this?.

    Trying to put in an array:
    ===================
    $output2 = array(stream_get_contents($output));
    print_r(array_count_values($output2));


    CODE:
    PHP Code:
    function logscrape3()
    {
     global 
    $username2$password2;

        
    $servers = array(

            array(
    'ip'=>'SERVERNAME','user'=>"$username2",'pass'=>"$password2"));


        foreach(
    $servers as $server)
        {
        
    $ips $server['ip'];
        
    $users $server['user'];
        
    $passes $server['pass'];
        
    $str1 ="error";
        
    $cmd1="grep -r '$str1' /opt/logs/";
              
         
    $connection ssh2_connect("$ips"22);
         
    ssh2_auth_password($connection"$users""$passes");
           if(
    $output ssh2_exec($connection"$cmd1;")) 
                {

            
    #TEST code to add each grep to an array    
            
    $Data stream_get_contents($output);
            
    $lines explode("\n"$Data);
            
            
    $arr= array($lines);
            foreach(
    $arr AS $line
            {
            
    print_r($line);

            }
             

            
    stream_set_blocking($outputtrue);
            echo 
    '<pre>';
        

                
            echo 
    '</pre>';


        
    fclose($output);
           }

        
    #print_r($server);

          
    }

    }
    #end fun 
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,689
    Rep Power
    6351
    What does this produce? Does it give you what you want?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0
    Hi,

    It lists the same error 100s times in each log file instead of just one e.g. with a count number of how many times it appears in the log.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,689
    Rep Power
    6351
    The EXACT SAME error?

    What SPECIFICALLY does this output?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0
    OUTPUT Example:

    10/04-00:00:00.075 [01] CHILD 1: error in Tcl evaluation:
    10/04-00:00:00.076 [01] CHILD 1: error in Tcl evaluation:
    10/04-00:00:00.346 [01] CHILD 0: error in Tcl evaluation:
    10/04-00:00:00.346 [01] CHILD 0: error in Tcl evaluation:
    10/04-00:00:00.347 [01] CHILD 0: error in Tcl evaluation:
    10/04-00:00:00.616 [01] CHILD 1: error in Tcl evaluation:
    10/04-00:00:00.616 [01] CHILD 1: error in Tcl evaluation:
    10/04-00:00:00.617 [01] CHILD 1: error in Tcl evaluation:
    10/04-00:00:00.886 [01] CHILD 0: error in Tcl evaluation:
    10/04-00:00:00.886 [01] CHILD 0: error in Tcl evaluation:
    10/04-00:00:00.887 [01] CHILD 0: error in Tcl evaluation:
    10/04-00:00:01.156 [01] CHILD 1: error in Tcl evaluation:
    10/04-00:00:01.157 [01] CHILD 1: error in Tcl evaluation:

    Expected Results:
    ==============

    10/04-00:00:01.157 [01] CHILD 1: error in Tcl evaluation: (found 13 times)
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,861
    Rep Power
    368
    what i would do is something like this:

    PHP Code:

    // error array
    // open the file and read it line by line
    //  strip the "10/04-00:00:00.075 [01]" as that is not needed. 
    //  check if CHILD 1: error in Tcl evaluation is in the error array
    // if it is then do  ++
    // else add it to that array. 
    [optionallynot sure if you need if/else and can get away with doing just $error[CHILD 1error in Tcl evaluation] ++; you MIGHT probably get a php notice error
  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,689
    Rep Power
    6351
    Well there's your problem right there: every line is different. You have to form your own uniqueness check if you want anything to happen here.

    Do you want to count just "error in Tcl evaluation" as the error, and count recurrences of that?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0
    Thanks, I updated my code above.

    My problem seems to be I am unable to get each grep -r into an array, it displays:

    Array
    (
    [0] =>
    )
  16. #9
  17. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,689
    Rep Power
    6351
    I thought it was working before? What changed?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0
    I am able to grep all files for the word "error" fine, however I am now trying to add all errors into an array which is producing 0 results.

    When I have all the grep results in an array I can then manipulate the data.

IMN logo majestic logo threadwatch logo seochat tools logo