22
33namespace SimpleSAML \Module \perun ;
44
5- use SimpleSAML \Logger ;
5+ use phpseclib3 \Crypt \RSA ;
6+ use phpseclib3 \Net \SSH2 ;
7+ use SimpleSAML \Error \Exception ;
8+
69
710/**
811 * Class sspmod_perun_NagiosStatusConnector
1114 */
1215class NagiosStatusConnector extends StatusConnector
1316{
14- const NAGIOS_URL = 'status.nagios.url ' ;
15- const NAGIOS_CERT_PATH = 'status.nagios.certificate_path ' ;
16- const NAGIOS_CERT_PASSWORD = 'status.nagios.certificate_password ' ;
17- const NAGIOS_CA_PATH = 'status.nagios.ca_path ' ;
18- const NAGIOS_PEER_VERIFY = 'status.nagios.peer_verification ' ;
19-
20- private $ url ;
21- private $ certPath ;
22- private $ certPassword ;
23- private $ caPath ;
24- private $ peerVerification ;
17+ protected const STATUS_NAGIOS = 'status_nagios ' ;
18+ protected const HOST = 'host ' ;
19+ protected const KEY_PATH = 'key_path ' ;
20+ protected const LOGIN = 'login ' ;
21+ protected const COMMAND = 'command ' ;
22+
23+ private $ host ;
24+ private $ keyPath ;
25+ private $ login ;
26+ private $ command ;
2527
2628 /**
2729 * NagiosStatusConnector constructor.
@@ -30,55 +32,50 @@ public function __construct()
3032 {
3133 parent ::__construct ();
3234
33- $ this ->url = $ this ->configuration ->getString (self ::NAGIOS_URL , '' );
34- $ this ->certPath = $ this ->configuration ->getString (self ::NAGIOS_CERT_PATH , '' );
35- $ this ->certPassword = $ this ->configuration ->getString (self ::NAGIOS_CERT_PASSWORD , '' );
36- $ this ->caPath = $ this ->configuration ->getString (self ::NAGIOS_CA_PATH , '' );
37- $ this ->peerVerification = $ this ->configuration ->getBoolean (self ::NAGIOS_PEER_VERIFY , false );
38-
39- if (empty ($ this ->url )) {
40- throw new \Exception ('Required option \'' . self ::NAGIOS_URL . '\' is empty! ' );
41- } elseif (empty ($ this ->certPath )) {
42- throw new \Exception ('Required option \'' . self ::NAGIOS_CERT_PATH . '\' is empty! ' );
43- } elseif (empty ($ this ->caPath )) {
44- throw new \Exception ('Required option \'' . self ::NAGIOS_CA_PATH . '\' is empty! ' );
35+ $ config = $ this ->configuration ->getConfigItem (self ::STATUS_NAGIOS , null );
36+
37+ if (is_null ($ this ->host )) {
38+ throw new Exception ('Property \'' . self ::STATUS_NAGIOS . '\' is missing or invalid! ' );
4539 }
46- }
4740
41+ $ this ->host = $ config ->getString (self ::HOST , null );
42+ $ this ->keyPath = $ config ->getString (self ::KEY_PATH , null );
43+ $ this ->login = $ config ->getString (self ::LOGIN , null );
44+ $ this ->command = $ config ->getString (self ::COMMAND , null );
45+
46+ if (empty ($ this ->host )) {
47+ throw new Exception ('Required option \'' . self ::HOST . '\' is empty! ' );
48+ } elseif (empty ($ this ->keyPath )) {
49+ throw new Exception ('Required option \'' . self ::KEY_PATH . '\' is empty! ' );
50+ } elseif (empty ($ this ->login )) {
51+ throw new Exception ('Required option \'' . self ::LOGIN . '\' is empty! ' );
52+ } elseif (empty ($ this ->command )) {
53+ throw new Exception ('Required option \'' . self ::COMMAND . '\' is empty! ' );
54+ }
55+ }
4856
4957 public function getStatus ()
5058 {
5159 $ result = [];
52- $ serviceStatuses = [];
53-
54- $ ch = curl_init ();
55- curl_setopt ($ ch , CURLOPT_URL , $ this ->url );
56- curl_setopt ($ ch , CURLOPT_VERBOSE , true );
57- curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , $ this ->peerVerification );
58- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
59- curl_setopt ($ ch , CURLOPT_SSLCERT , $ this ->certPath );
60- curl_setopt ($ ch , CURLOPT_CAPATH , $ this ->caPath );
61- curl_setopt ($ ch , CURLOPT_SSLKEYPASSWD , $ this ->certPassword );
62-
63- $ response = curl_exec ($ ch );
6460
65- if ($ response === false ) {
66- Logger:: error ( curl_error ( $ ch ) );
61+ if (!( $ key = file_get_contents ( $ this -> keyPath )) ) {
62+ throw new Exception ( ' Cannot load ket from path: \'' . $ this -> keyPath . '\' ! ' );
6763 }
6864
69- curl_close ($ ch );
65+ $ key = RSA ::load ($ key );
66+ $ ssh = new SSH2 ($ this ->host );
7067
71- $ jsonResponse = json_decode ($ response , true );
72-
73- if (isset ($ jsonResponse ['status ' ]['service_status ' ])) {
74- $ serviceStatuses = $ jsonResponse ['status ' ]['service_status ' ];
68+ if (!$ ssh ->login ($ this ->login , $ key )) {
69+ throw new Exception ('Error during ssh connection to \'' . $ this ->login . '@ ' . $ this ->host . '\' ! ' );
7570 }
7671
77- foreach ($ serviceStatuses as $ serviceStatus ) {
78- $ status = [];
79- $ status ['name ' ] = $ serviceStatus ['service_display_name ' ];
80- $ status ['status ' ] = $ serviceStatus ['status ' ];
81- array_push ($ result , $ status );
72+ $ output = $ ssh ->exec ($ this ->command );
73+ $ lines = explode ("\n" , $ output );
74+ array_pop ($ lines );
75+
76+ foreach ($ lines as $ line ) {
77+ $ lineParts = explode ("; " , $ line );
78+ $ result [$ lineParts [0 ]][$ lineParts [1 ]] = $ lineParts [2 ];
8279 }
8380
8481 return $ result ;
0 commit comments