예제 #1
0
 private function attachAndSendEvents($numEvents, $indexDelay)
 {
     list($service, $indexName, $index, $expectedEvents, $data) = $this->makeEvents($numEvents);
     // Submit events
     $eventOutputStream = $service->getReceiver()->attach(array('index' => $indexName, 'sourcetype' => 'php_unit_test'));
     Splunk_Util::fwriteall($eventOutputStream, $data);
     fclose($eventOutputStream);
     $this->checkEvents($service, $indexName, $index, $expectedEvents, $numEvents, $indexDelay);
 }
예제 #2
0
 public function testAttach()
 {
     $service = $this->loginToRealService();
     $index = $service->getIndexes()->get('_internal');
     // (Only test that this code path appears to work.
     //  The ReceiverTest checks whether submitted events actually show up.)
     $eventOutputStream = $index->attach(array('sourcetype' => 'php_unit_test'));
     Splunk_Util::fwriteall($eventOutputStream, 'DELETEME');
     fclose($eventOutputStream);
 }
예제 #3
0
 /**
  * @return resource     A stream that reads from the specified byte string.
  */
 public static function create($string)
 {
     $stream = fopen('php://memory', 'rwb');
     Splunk_Util::fwriteall($stream, $string);
     fseek($stream, 0);
     /*
      * fseek() causes the next call to feof() to always return FALSE,
      * which is undesirable if we seeked to the EOF. In this case,
      * attempt a read past EOF so that the next call to feof() returns
      * TRUE as expected.
      */
     if ($string === '') {
         fread($stream, 1);
     }
     // trigger EOF explicitly
     return $stream;
 }
예제 #4
0
 /**
  * Creates a stream for logging events to the specified index.
  * 
  * In addition to the index name it is highly recommended to specify
  * a sourcetype explicitly.
  * 
  * The returned stream should eventually be closed via fclose().
  * 
  * @param array $args   (optional) {<br/>
  *      **host**: (optional) The value to populate in the host field
  *                for events from this data input.<br/>
  *      **host_regex**: (optional) A regular expression used to
  *                      extract the host value from each event.<br/>
  *      **index**: (optional) The index to send events from this
  *                 input to. Highly recommended. Defaults to "default".<br/>
  *      **source**: (optional) The source value to fill in the
  *                  metadata for this input's events.<br/>
  *      **sourcetype**: (optional) The sourcetype to apply to
  *                      events from this input.<br/>
  * }
  * @return resource     A stream that you can write event text to.
  * @throws Splunk_IOException
  * @link http://docs.splunk.com/Documentation/Splunk/latest/RESTAPI/RESTinput#receivers.2Fstream
  */
 public function attach($args = array())
 {
     $scheme = $this->service->getScheme();
     $host = $this->service->getHost();
     $port = $this->service->getPort();
     $errno = 0;
     $errstr = '';
     if ($scheme == 'http') {
         $stream = @fsockopen($host, $port, $errno, $errstr);
     } else {
         if ($scheme == 'https') {
             $stream = @fsockopen('ssl://' . $host, $port, $errno, $errstr);
         } else {
             throw new Splunk_UnsupportedOperationException('Unsupported URL scheme.');
         }
     }
     if ($stream === FALSE) {
         throw new Splunk_ConnectException($errstr, $errno);
     }
     $path = '/services/receivers/stream?' . http_build_query($args);
     $token = $this->service->getToken();
     $headers = array("POST {$path} HTTP/1.1\r\n", "Host: {$host}:{$port}\r\n", "Accept-Encoding: identity\r\n", "Authorization: {$token}\r\n", "X-Splunk-Input-Mode: Streaming\r\n", "\r\n");
     Splunk_Util::fwriteall($stream, implode('', $headers));
     return $stream;
 }
예제 #5
0
 /**
  * Creates a stream for logging events to the specified index.
  * 
  * In addition to the index name it is highly recommended to specify
  * a sourcetype explicitly.
  * 
  * The returned stream should eventually be closed via fclose().
  * 
  * @param array $args   (optional) {<br/>
  *      **host**: (optional) The value to populate in the host field
  *                for events from this data input.<br/>
  *      **host_regex**: (optional) A regular expression used to
  *                      extract the host value from each event.<br/>
  *      **index**: (optional) The index to send events from this
  *                 input to. Highly recommended. Defaults to "default".<br/>
  *      **source**: (optional) The source value to fill in the
  *                  metadata for this input's events.<br/>
  *      **sourcetype**: (optional) The sourcetype to apply to
  *                      events from this input.<br/>
  * }
  * @return resource     A stream that you can write event text to.
  * @throws Splunk_IOException
  * @link http://docs.splunk.com/Documentation/Splunk/latest/RESTAPI/RESTinput#receivers.2Fstream
  */
 public function attach($args = array())
 {
     $scheme = $this->service->getScheme();
     $host = $this->service->getHost();
     $port = $this->service->getPort();
     $errno = 0;
     $errstr = '';
     $socketContext = stream_context_create(array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false)));
     if ($scheme == 'http') {
         $stream = @stream_socket_client($host . ":" . $port, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $socketContext);
     } else {
         if ($scheme == 'https') {
             $stream = @stream_socket_client('ssl://' . $host . ":" . $port, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $socketContext);
         } else {
             throw new Splunk_UnsupportedOperationException('Unsupported URL scheme.');
         }
     }
     if ($stream === FALSE) {
         throw new Splunk_ConnectException("error: " . $errstr . $host . ":" . $port, $errno);
     }
     $path = '/services/receivers/stream?' . http_build_query($args);
     $token = $this->service->getToken();
     $headers = array("POST {$path} HTTP/1.1\r\n", "Host: {$host}:{$port}\r\n", "Accept-Encoding: identity\r\n", "Authorization: {$token}\r\n", "X-Splunk-Input-Mode: Streaming\r\n", "\r\n");
     Splunk_Util::fwriteall($stream, implode('', $headers));
     return $stream;
 }