1313use ipl \Stdlib \Filter ;
1414use ipl \Web \Url ;
1515
16+ /**
17+ * IcingadbimgController loads graphs as images from the grafana-image-renderer.
18+ */
1619class IcingadbimgController extends IcingadbGrafanaController
1720{
1821 protected $ host ;
@@ -21,26 +24,26 @@ class IcingadbimgController extends IcingadbGrafanaController
2124 protected $ myConfig ;
2225 protected $ myAuth ;
2326 protected $ authentication ;
24- protected $ grafanaHost = null ;
25- protected $ grafanaTheme = null ;
26- protected $ protocol = "http " ;
27- protected $ username = null ;
28- protected $ password = null ;
29- protected $ apiToken = null ;
30- protected $ width = 640 ;
31- protected $ height = 280 ;
32- protected $ defaultDashboard = "icinga2-default " ;
33- protected $ defaultDashboardPanelId = "1 " ;
34- protected $ defaultOrgId = "1 " ;
35- protected $ shadows = false ;
36- protected $ dataSource = null ;
37- protected $ proxyTimeout = "5 " ;
38- protected $ custvarconfig = "grafana_graph_config " ;
39- protected $ SSLVerifyPeer = false ;
40- protected $ SSLVerifyHost = "0 " ;
41- protected $ cacheTime = 300 ;
27+ protected $ grafanaHost = null ;
28+ protected $ grafanaTheme = null ;
29+ protected $ protocol = "http " ;
30+ protected $ username = null ;
31+ protected $ password = null ;
32+ protected $ apiToken = null ;
33+ protected $ width = 640 ;
34+ protected $ height = 280 ;
4235 protected $ defaultdashboarduid ;
43- protected $ enableCache = "yes " ;
36+ protected $ defaultDashboard = "icinga2-default " ;
37+ protected $ defaultDashboardPanelId = "1 " ;
38+ protected $ defaultOrgId = "1 " ;
39+ protected $ shadows = false ;
40+ protected $ dataSource = null ;
41+ protected $ proxyTimeout = "5 " ;
42+ protected $ custvarconfig = "grafana_graph_config " ;
43+ protected $ SSLVerifyPeer = false ;
44+ protected $ SSLVerifyHost = "0 " ;
45+ protected $ cacheTime = 300 ;
46+ protected $ enableCache = "yes " ;
4447 protected $ customVars ;
4548 protected $ timerangeto ;
4649 protected $ object ;
@@ -49,17 +52,21 @@ class IcingadbimgController extends IcingadbGrafanaController
4952 protected $ panelId ;
5053 protected $ orgId ;
5154
55+ /**
56+ * Mainly loading defaults and the configuration.
57+ */
5258 public function init ()
5359 {
5460 if (! $ this ->useIcingadbAsBackend ) {
5561 $ this ->redirectNow (Url::fromPath ('grafana/dashboard ' )->setQueryString ($ this ->params ));
5662 }
57- /* we need at least a host name */
63+
64+ // We need at least a host name
5865 if (is_null ($ this ->getParam ('host ' ))) {
5966 throw new \Error ('No host given! ' );
6067 }
6168
62- /* save timerange from params for later use */
69+ // Save timerange from params for later use
6370 $ this ->timerange = $ this ->hasParam ('timerange ' ) ? urldecode ($ this ->getParam ('timerange ' )) : null ;
6471 if ($ this ->hasParam ('timerangeto ' )) {
6572 $ this ->timerangeto = urldecode ($ this ->getParam ('timerangeto ' ));
@@ -70,16 +77,18 @@ public function init()
7077 // Get the cachetime URL parameter and use the default if not present
7178 $ this ->cacheTime = $ this ->hasParam ('cachetime ' ) ? $ this ->getParam ('cachetime ' ) : $ this ->cacheTime ;
7279
73- /* load global configuration */
80+ // Load global configuration
7481 $ this ->myConfig = Config::module ('grafana ' )->getSection ('grafana ' );
7582 $ this ->grafanaHost = $ this ->myConfig ->get ('host ' , $ this ->grafanaHost );
7683 if ($ this ->grafanaHost == null ) {
7784 throw new ConfigurationError (
7885 'No Grafana host configured! '
7986 );
8087 }
88+
8189 $ this ->protocol = $ this ->myConfig ->get ('protocol ' , $ this ->protocol );
8290
91+ // Get defaults for Grafana dashboards
8392 $ this ->defaultDashboard = $ this ->myConfig ->get ('defaultdashboard ' , $ this ->defaultDashboard );
8493 $ this ->defaultdashboarduid = $ this ->myConfig ->get ('defaultdashboarduid ' , null );
8594 if (is_null ($ this ->defaultdashboarduid )) {
@@ -91,43 +100,27 @@ public function init()
91100 'defaultdashboardpanelid ' ,
92101 $ this ->defaultDashboardPanelId
93102 );
103+
94104 $ this ->defaultOrgId = $ this ->myConfig ->get ('defaultorgid ' , $ this ->defaultOrgId );
95105 $ this ->grafanaTheme = Util::getUserThemeMode (Auth::getInstance ()->getUser ());
96106 $ this ->height = $ this ->myConfig ->get ('height ' , $ this ->height );
97107 $ this ->width = $ this ->myConfig ->get ('width ' , $ this ->width );
98108 $ this ->proxyTimeout = $ this ->myConfig ->get ('proxytimeout ' , $ this ->proxyTimeout );
99109 $ this ->enableCache = $ this ->myConfig ->get ('enablecache ' , $ this ->enableCache );
100- /**
101- * Read the global default timerange
102- */
110+
111+ // Read the global default timerange
103112 if ($ this ->timerange == null ) {
104113 $ this ->timerange = $ this ->config ->get ('timerange ' , $ this ->timerange );
105114 }
106- /**
107- * Datasource needed to regex special chars
108- */
115+
116+ // Datasource needed to regex special chars
109117 $ this ->dataSource = $ this ->myConfig ->get ('datasource ' , $ this ->dataSource );
110- /**
111- * Display shadows around graph
112- */
113118 $ this ->shadows = $ this ->myConfig ->get ('shadows ' , $ this ->shadows );
114- /**
115- * Name of the custom variable for graph config
116- */
117119 $ this ->custvarconfig = ($ this ->myConfig ->get ('custvarconfig ' , $ this ->custvarconfig ));
118- /**
119- * Verify the certificate's name against host
120- */
121120 $ this ->SSLVerifyHost = ($ this ->myConfig ->get ('ssl_verifyhost ' , $ this ->SSLVerifyHost ));
122- /**
123- * Verify the peer's SSL certificate
124- */
125121 $ this ->SSLVerifyPeer = ($ this ->myConfig ->get ('ssl_verifypeer ' , $ this ->SSLVerifyPeer ));
126122
127- /**
128- * Username & Password or token
129- */
130-
123+ // Username & Password or token
131124 $ this ->apiToken = $ this ->myConfig ->get ('apitoken ' , $ this ->apiToken );
132125 $ this ->authentication = $ this ->myConfig ->get ('authentication ' );
133126 if ($ this ->apiToken == null && $ this ->authentication == "token " ) {
@@ -192,7 +185,8 @@ public function indexAction()
192185 $ this ->customVars = str_replace ($ search , $ replace , $ this ->customVars );
193186 }
194187
195- // urlencode values
188+ // urlencode custom variable values
189+ // TODO: Could this be a Util function?
196190 $ customVars = "" ;
197191 foreach (preg_split ('/\&/ ' , $ this ->customVars , -1 , PREG_SPLIT_NO_EMPTY ) as $ param ) {
198192 $ arr = explode ("= " , $ param );
@@ -206,7 +200,8 @@ public function indexAction()
206200 }
207201 $ this ->customVars = $ customVars ;
208202 }
209- // replace special chars for graphite
203+
204+ // Replace special chars for graphite
210205 if ($ this ->dataSource == "graphite " ) {
211206 $ serviceName = Util::graphiteReplace ($ serviceName );
212207 $ hostName = Util::graphiteReplace ($ hostName );
@@ -225,8 +220,8 @@ public function indexAction()
225220 $ string = wordwrap ($ this ->translate ('Error ' ). ': ' . $ imageHtml , 40 , "\n" );
226221 $ lines = explode ("\n" , $ string );
227222 $ im = @imagecreate ($ this ->width , $ this ->height );
228- $ background_color = imagecolorallocate ($ im , 255 , 255 , 255 ); //white background
229- $ text_color = imagecolorallocate ($ im , 255 , 0 , 0 );//black text
223+ $ background_color = imagecolorallocate ($ im , 255 , 255 , 255 );
224+ $ text_color = imagecolorallocate ($ im , 255 , 0 , 0 );
230225 foreach ($ lines as $ i => $ line ) {
231226 imagestring ($ im , 5 , 0 , 5 + $ i * 15 , $ line , $ text_color );
232227 }
@@ -275,6 +270,7 @@ private function setGraphConf($serviceName, $serviceCommand = null)
275270 private function getMyimageHtml ($ serviceName , $ hostName , &$ imageHtml )
276271 {
277272 $ imgClass = $ this ->shadows ? "grafana-img grafana-img-shadows " : "grafana-img " ;
273+
278274 // Test whether curl is loaded
279275 if (extension_loaded ('curl ' ) === false ) {
280276 $ imageHtml = $ this ->translate ('CURL extension is missing. '
@@ -352,6 +348,7 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
352348
353349 curl_close ($ curl_handle );
354350 $ imageHtml = $ result ;
351+
355352 return true ;
356353 }
357354}
0 commit comments