1 | #!/usr/local/bin/php |
---|
2 | <?php |
---|
3 | |
---|
4 | $_REQUEST = json_decode( $argv[ 1 ], true ); |
---|
5 | |
---|
6 | __~debug:filemanager{error_log( print_r( $_REQUEST, true ) , 3, "/tmp/mylog" );} |
---|
7 | |
---|
8 | $results = []; |
---|
9 | |
---|
10 | if ( !sizeof( $_REQUEST ) ) { |
---|
11 | $results[ 'error' ] = "PHP code received no \$_REQUEST?"; |
---|
12 | echo (json_encode($results)); |
---|
13 | exit(); |
---|
14 | } |
---|
15 | |
---|
16 | if ( !isset( $_REQUEST[ '_uuid' ] ) ) { |
---|
17 | $results[ "error" ] = "No _uuid specified in the request"; |
---|
18 | echo (json_encode($results)); |
---|
19 | exit(); |
---|
20 | } |
---|
21 | |
---|
22 | if ( !isset( $_REQUEST[ '_logon' ] ) ) { |
---|
23 | $results[ "error" ] = "No _logon specified in the request"; |
---|
24 | echo (json_encode($results)); |
---|
25 | exit(); |
---|
26 | } |
---|
27 | |
---|
28 | if ( !isset( $_REQUEST[ 'interval' ] ) ) { |
---|
29 | $results[ "error" ] = "Insufficient request data"; |
---|
30 | echo (json_encode($results)); |
---|
31 | exit(); |
---|
32 | } |
---|
33 | |
---|
34 | $appconfig = json_decode( file_get_contents( "__appconfig__" ) ); |
---|
35 | |
---|
36 | if ( !isset( $appconfig->messaging->zmqhostip ) || |
---|
37 | !isset( $appconfig->messaging->zmqport ) || |
---|
38 | !isset( $appconfig->admin ) ) { |
---|
39 | $results[ "error" ] = "appconfig.json missing zmq or admin info"; |
---|
40 | echo (json_encode($results)); |
---|
41 | exit(); |
---|
42 | } |
---|
43 | |
---|
44 | if ( !in_array( $_REQUEST[ '_logon' ], $appconfig->admin ) ) { |
---|
45 | $results[ "error" ] = "not an administrator"; |
---|
46 | echo (json_encode($results)); |
---|
47 | exit(); |
---|
48 | } |
---|
49 | |
---|
50 | date_default_timezone_set("UTC"); |
---|
51 | |
---|
52 | $context = new ZMQContext(); |
---|
53 | $zmq_socket = $context->getSocket(ZMQ::SOCKET_PUSH, '__application__ udp pusher'); |
---|
54 | $zmq_socket->connect("tcp://" . $appconfig->messaging->zmqhostip . ":" . $appconfig->messaging->zmqport ); |
---|
55 | |
---|
56 | function db_connect( $error_json_exit = false ) { |
---|
57 | global $use_db; |
---|
58 | global $db_errors; |
---|
59 | |
---|
60 | if ( !isset( $use_db ) ) { |
---|
61 | try { |
---|
62 | $use_db = new MongoClient(); |
---|
63 | } catch ( Exception $e ) { |
---|
64 | $db_errors = "Could not connect to the db " . $e->getMessage(); |
---|
65 | if ( $error_json_exit ) |
---|
66 | { |
---|
67 | $results = array( "error" => $db_errors ); |
---|
68 | $results[ '_status' ] = 'complete'; |
---|
69 | echo (json_encode($results)); |
---|
70 | exit(); |
---|
71 | } |
---|
72 | return false; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | return true; |
---|
77 | } |
---|
78 | |
---|
79 | // get_runinfo populates runinfo array with job info |
---|
80 | // to get more data => add it to the runinfo |
---|
81 | |
---|
82 | function get_runinfo( $error_json_exit = false ) { |
---|
83 | global $use_db; |
---|
84 | global $db_errors; |
---|
85 | global $appconfig; |
---|
86 | global $runinfo; |
---|
87 | |
---|
88 | $runinfo = []; |
---|
89 | |
---|
90 | if ( !db_connect( $error_json_exit ) ) |
---|
91 | { |
---|
92 | return false; |
---|
93 | } |
---|
94 | |
---|
95 | $runs = $use_db->__application__->running->find(); |
---|
96 | |
---|
97 | foreach ( $runs as $v ) { |
---|
98 | $uuid = $v['_id']; |
---|
99 | $job = $use_db->__application__->jobs->findOne( array( "_id" => $uuid ) ); |
---|
100 | $pids = $v['pid']; |
---|
101 | |
---|
102 | $resources = []; |
---|
103 | |
---|
104 | foreach ( $pids as $k2 => $v2 ) { |
---|
105 | $resources[ $v2['where'] ] = true; |
---|
106 | // later get pid info |
---|
107 | // echo " where: " . $v2['where'] . " pid: " . $v2['pid'] . " what: " . $v2['what'] . "\n"; |
---|
108 | // $cmd = $appconfig->resources->{ $v2['where'] } . " ps --ppid " . $v2['pid']; |
---|
109 | // echo " cmd $cmd\n"; |
---|
110 | } |
---|
111 | |
---|
112 | $runinfo[] = |
---|
113 | array( |
---|
114 | "module" => $job[ 'module' ] |
---|
115 | ,"user" => $job[ 'user' ] |
---|
116 | # "pids" => $v [ 'pid' ] |
---|
117 | ,"started" => date( "Y M d H:i:s T",$job["start"]->sec ) |
---|
118 | ,"resources" => implode( ",", array_keys( $resources ) ) |
---|
119 | ,"id" => $uuid |
---|
120 | ); |
---|
121 | } |
---|
122 | return true; |
---|
123 | } |
---|
124 | |
---|
125 | function get_html_runinfo( $error_json_exit = false ) { |
---|
126 | global $runinfo; |
---|
127 | global $html_runinfo; |
---|
128 | |
---|
129 | $html_runinfo = "No jobs running"; |
---|
130 | |
---|
131 | if ( !get_runinfo( $error_json_exit ) ) { |
---|
132 | return false; |
---|
133 | } |
---|
134 | |
---|
135 | if ( !count( $runinfo ) ) { |
---|
136 | return true; |
---|
137 | } |
---|
138 | |
---|
139 | $html_runinfo = "<table><tr><th>" . implode( "</th><th>", array_keys( $runinfo[ 0 ] ) ) . "</th></tr>"; |
---|
140 | |
---|
141 | foreach ( $runinfo as $k => $v ) { |
---|
142 | $html_runinfo .= "<tr><td>" . implode( "</td><td>", $v ) . "</td></tr>"; |
---|
143 | } |
---|
144 | |
---|
145 | $html_runinfo .= "</table>"; |
---|
146 | } |
---|
147 | |
---|
148 | $results = []; |
---|
149 | $results[ '_uuid' ] = $_REQUEST[ '_uuid' ]; |
---|
150 | |
---|
151 | // run until cancelled |
---|
152 | do { |
---|
153 | get_html_runinfo( true ); |
---|
154 | |
---|
155 | $results[ "monitordata" ] = "<p>Last refreshed " . date( "Y M d H:i:s T", time() ) . "</p>" . $html_runinfo; |
---|
156 | |
---|
157 | $zmq_socket->send( json_encode( $results ) ); |
---|
158 | sleep( $_REQUEST[ 'interval' ] ); |
---|
159 | } while(1); |
---|
160 | |
---|
161 | ?> |
---|