Advertisement
Guest User

splunk_anon

a guest
Dec 18th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.93 KB | None | 0 0
  1. <?php
  2.  
  3. // Import Splunk.php
  4. require_once 'Splunk.php';
  5.  
  6. // Create an instance of Splunk_Service to connect to a Splunk server
  7. $service = new Splunk_Service(array(
  8.     'host' => 'data.intra.a-tono.net',
  9.     'port' => '8089',
  10.     'username' => '',
  11.     'password' => '',
  12. ));
  13.  
  14. //Configure Search
  15.  
  16. $searchQueryNormal = 'search index=droppay port=11116 event=message_MO | fields ts,msisdn,text ';
  17. $searchParams = array(
  18.     'exec_mode' => 'normal',
  19.     'earliest_time' => '2013-07-01T00:00:00.000-00:00',
  20.     'latest_time' => '2015-01-01T00:00:00.000-00:00'
  21. );
  22.  
  23. // Log into the Splunk service
  24. $service->login();
  25.  
  26.  
  27. // Run a normal search        
  28. $job = $service->getJobs()->create($searchQueryNormal, $searchParams);
  29.  
  30. try
  31.   {
  32.     // Print progress of the job as it is running
  33.     echo "Search progress...\n";
  34.     while (!$job->isDone())
  35.     {
  36.       printf("%03.1f%%", $job->getProgress() * 100);
  37.       echo "\n";
  38.       flush();
  39.  
  40.       usleep(0.5 * 1000000);
  41.       $job->refresh();
  42.     }
  43.     echo "Done!\n";
  44.  
  45.     // Get job results
  46.     $resultsNormalSearch = $job->getResults();
  47.     $messages = array();
  48.   }
  49. catch (Exception $e)
  50.   {
  51.     // Generate fake result that contains the exception message
  52.     $resultsNormalSearch = array();
  53.     $messages = array();
  54.     $messages[] = new Splunk_ResultsMessage('EXCEPTION', $e->getMessage());
  55.   }
  56.  
  57. // Use the built-in XML parser to display the job results
  58. foreach ($resultsNormalSearch as $result)
  59.   {
  60.    
  61.       $result_array = (array)$result;
  62.       $ts = $result_array["ts"];
  63.       $source = substr($result_array["msisdn"], 2);
  64.       $text = $result_array["text"];
  65.       $dest = preg_split("/[\s,]+/", $text)[1];
  66.       $filename = "anon_" . str_replace("-", "", substr($ts, 0, 7)) . ".tx";
  67.       $row = "$ts,$source,$dest\n";
  68.  
  69.       $fh=fopen($filename,"a");
  70.       fwrite($fh, $row);
  71.       fclose($fh);
  72.  
  73.       echo "Inserted: $row in $filename\n";
  74.      
  75.  
  76.   }
  77.  
  78. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement