Ver código fonte

Se crea el servicio para leer valores del MySQL del StatsD mediando el
json endpoint mysql.

Maximiliano Schvindt 7 anos atrás
pai
commit
e0932ac187

+ 42 - 0
Command/EndpointMysqlCommand.php

@@ -0,0 +1,42 @@
+<?php
+
+namespace StatsDBundle\Command;
+
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class EndpointMysqlCommand extends ContainerAwareCommand
+{
+
+    protected function configure()
+    {
+        $this
+                ->setName('endpoint:mysql')
+                ->setDescription('Test jsonendpoint mysql')
+        ;
+    }
+
+    /**
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $endpoint = $this->getContainer()->get('endpoint.mysql');
+
+        $json = '{"range":{"from":"2017-11-13T14:09:25.307Z","to":"2017-11-13T17:09:25.308Z"},"intervalMs":0,"targets":[{"target":"d_3_s_1_snmp_ponRxPower"}],"format":"json","maxDataPoints":10}';
+        
+        $result = $endpoint->get($json,'last');
+        
+        //$result = $endpoint->get($json,'query');
+
+        print_r($result);
+        
+        print_r(PHP_EOL);
+        print_r(date("Y-m-d H:i:s"));
+        print_r(PHP_EOL);
+    }
+
+}

+ 4 - 1
Resources/config/services.yml

@@ -1,3 +1,6 @@
 services:
     statsd:
-        class: StatsDBundle\Services\StatsD
+        class: StatsDBundle\Services\StatsD
+    endpoint.mysql:
+        class: StatsDBundle\Services\EndpointMysql
+        arguments: ["@service_container"]

+ 3 - 2
Resources/config/statsd.ini

@@ -1,3 +1,4 @@
 [statsd]
-host = 127.0.0.1
-port = 8125
+host = 200.50.168.118
+port = 8125
+[endpoint.mysql]

+ 67 - 0
Services/EndpointMysql.php

@@ -0,0 +1,67 @@
+<?php
+
+namespace StatsDBundle\Services;
+
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Read statistics to the statsD
+ */
+class EndpointMysql
+{
+    /**
+     * @var ContainerInterface
+     */
+    protected $serviceContainer;
+    protected $location;
+    protected $query;
+
+    /**
+     * @param ContainerInterface $serviceContainer
+     */
+    public function __construct(ContainerInterface $serviceContainer)
+    {
+        $this->serviceContainer = $serviceContainer;
+
+        if($this->serviceContainer->getParameter("statsd.connection")) {
+            $this->location = $this->serviceContainer->getParameter("statsd.connection");
+        } else {
+            $this->location = null;
+        } 
+    }
+
+    /**
+     * @param string $json
+     * @param string $query
+     * query = [last|query|search]
+     */
+    
+    public function get($json, $method = "query") 
+    {
+        if(is_null($this->location)) return array();
+        
+        $ch = curl_init();
+        
+        curl_setopt($ch, CURLOPT_URL, "{$this->location}/{$method}");
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
+        curl_setopt($ch, CURLOPT_POST, 1);
+
+        $headers = array();
+        $headers[] = "Content-Type: application/json";
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+
+        $result = curl_exec($ch);
+        if (curl_errno($ch)) {
+            echo 'Error:' . curl_error($ch);
+        }
+        curl_close ($ch);
+
+        $data = json_decode($result,true);
+        if(is_array($data)) 
+            return $data;
+
+        return array();
+    }
+
+}