RTLogins

From Request Tracker Wiki
Jump to navigation Jump to search

Introduction

This is a very basic and minimal php script I wrote to output the logins captured in syslog. It just dumps it to screen but you can redirect the output to a file also.

RT Login Stats for 2/9/2005
eranam,1
wildmanj,6
garcial,3
timm,5
reillyj,4
JenningsR,1
veaziec,2
vongleichr,1
lewisd,1

Requirements

I wrote this on PHP4 with PEAR::Date used. You'll have to change the file location of the pear lib to where yours resides.

Caveat Emptor

This script is provided with no warranties expressed or implied. Use your own risk. Milage may vary.

TODO

  • Make a cron version
  • Have script store to RRD
  • Have summary e-mailed
#!/usr/bin/php -q
 <?php
 //
 // RTLogins.php
 //
 // parses syslog for RT Login information
 // returns username,#logins
 
 // You're going to want to re-point this to where pear is on your machine
 include_once("/var/www/tikiwiki/lib/pear/Date/Calc.php");
 
 $date_calc = new Date_Calc;
 
 // $logpath = "/var/log/";
 $logfile = "";
 $buffer = "";
 $meDebug = False;
 $debug_routine_name = "main";
 $login_regex = 'Successful login';
 $counter = 0;
 $res_summary = array();
 
 // What's in argv[1]?
 if (strlen($argv[1] < 2)){
     $logfile = $argv[1];
     } else {
         $logfile = "syslog";
         }
 
 // is this a debug session?
 if (debug_set($argv[1])){
     $meDebug = True;
     }
 
 // open the file for reading
 if(! $handle=openFile($logfile)){
     if ($meDebug){echo $debug_routine_name . ": Premature Termination.\n";}
     die;
     }
 
 // Loop through logfile
 while (!feof($handle)) {
 
    $buffer = fgets($handle, 4096);
 
     // Match for Login Success
     $foundLogin = strpos($buffer,$login_regex);
     if ($meDebug){echo $buffer . " Match to " . $login_regex . " \n";}
 
     $counter++;
 
     // Is current line a match?
     if($foundLogin >= 1){
 
         if ($meDebug){echo $counter . " : Found a match \n";}
         list($month,
              $nonce3,
              $day,
              $time,
              $server,
              $app,
              $result1,
              $result2,
              $nonce1,
              $user,
              $nonce2,
              $client,
              $func_str)=explode(" ",$buffer);
 
          if ($meDebug){
             echo "Month is " . $month . "\n";
             echo "Day is " . $day . "\n";
             echo "Time is " . $time . "\n";
             echo "Server is " . $server . "\n";
             echo "App is " . $app . "\n";
             echo "result1 is " . $result1 . "\n";
             echo "result2 is " . $result2 . "\n";
             echo "Nonce1 is " . $nonce1 . "\n";
             echo "Client is " . $client . "\n";
             echo "Nonce2 is " . $func_str . "\n";
             }
 
          // If the users array key has not been set, go set it
          if (! isset($res_array[$user])){
             $res_array[$user] = array();
             $res_array[$user]['Times']=array();
             }
 
          $month = $date_calc->getMonthFromFullName($month);
          //$time = str_replace(":","",$time);
          $res_array[$user]['Times'][] = substr(100+$day,1) . "/" . substr(100+$month,1) . "/2005 " . $time;
 
         }
 
 }
 
 
 // Dump result
 echo "RT Login Stats for " . $day . "/" . $month . "/2005\n";
 dumpResult($res_array);
 echo "\n";
 
 function dumpResult($result){
 
     foreach($result as $name => $times){
 
         echo $name . "," . count($result[$name]["Times"]);
         echo "\n";
 
         }
 
     return True;
     }
 
 function openFile($fn){
 
     $logfile = $fn;
 
     // echo "Opening the File $logfile \n";
 
     if(! $handle = fopen($logfile, "r")){
         echo "Couldn't open logfile for reading!\n";
         die;
         }
     return $handle;
     }
 
 function debug_set($isDebug){
     $isDebug=strtolower($isDebug);
     // echo "In the debug setting\nValue is $isDebug\n\n";
     if($isDebug=='y'){
         Return True;
         }
     }
 
 
 ?>
 
 

MichaelErana g33k (at) efamilynj.org