Odyssey
getcus.prg
1 <?php
2  // vim: tabstop=2 expandtab syn=php
3 
4 require_once 'logging.i';
5 require_once 'hcuEnv.i';
6 require_once 'hcuCommon.i';
7 // require_once(dirname(__FILE__) . '/../../shared/library/hcuCommon.i');
8 
9 $logger = new ColorStandardLogger('getcus');
10 $env = LoadSystemEnv('version');
11 
12 if ($env['devmode']) {
13  $env['logger']->warning("Showing /getcus info - DEVMODE.");
14  $myhostname = "localhost:80";
15  $getcustatusvalues = array("stage.homecu.io", "http://" . $myhostname . "/cumaps/getcustatus.prg");
16 } else {
17  $myhostname = "my.homecu.net";
18  $getcustatusvalues = array("my.homecu.net", "https://" . $myhostname . "/cumaps/getcustatus.prg");
19 }
20 // $env['logger']->warning("myhostname " . $myhostname);
21 // $env['logger']->warning("getcustatus " . $getcustatusurl);
22 $HB_ENV = LoadSystemEnv("banking");
23 $db = $HB_ENV['db'];
24 
25 $connect_to = sprintf('host=%s port=%d dbname=%s', $db['host'],
26  $db['port'], $db['dbname']);
27 if (!is_null($db['user'])) {
28  $connect_to .= ' user=' . $db['user'];
29 }
30 if (!is_null($db['connect_timeout'])) {
31  $connect_to .= ' connect_timeout=' . $db['connect_timeout'];
32 }
33 if (!is_null($db['password'])) {
34  $connect_to .= ' password=' . $db['password'];
35 }
36 $r = pg_connect($connect_to);
37 
38 if (!$r) {
39  throw new Exception('DB connect error');
40 }
41 if (pg_connection_status($r) !== PGSQL_CONNECTION_OK) {
42  throw new Exception('DB status error');
43 }
44 if (!pg_ping($r)) {
45  throw new Exception('DB ping error');
46 }
47 
48 //culist from cuinfo table
49 $result = pg_query($r, "select user_name, name, vendor, geo_lat, geo_long, dec_31_assets, dec_31_mem, www_server, system_options from cuinfo where ((system_options & 64) = 0) order by user_name");
50 
51 $res = array(); //gather the result that will be output
52 $fields = array( "name", "cuname", "vendor", "lat", "lng", "assets", "members", "www_server" );
53 
54 while ($row = pg_fetch_row($result)) {
55  $row[0] = trim($row[0]);
56  $entry = array();
57  $name = $row[0]; //data for each of the cu's
58  for ($i=0; $i<count($fields); $i++){
59  $entry[$fields[$i]] = trim($row[$i]); //match the values to field names
60  };
61  $res[$name] = $entry; //store the values for this cu
62 };
63 
64 //get status data from each server
65 //results return in the form:
66 // {
67 // "cruisecu":{"livebatch":"L","offlinestat":""},
68 // "scrubcu":{"livebatch":"B","offlinestat":"N"}
69 // }
70 foreach( array(
71  array("www5", 'https://nobody:no1home@www5.homecu.net/hcuadm/getcustatus'),
72  array("www3", 'https://nobody:no1home@www3.homecu.net/hcuadm/getcustatus'),
73  array("www6", 'https://nobody:no1home@www6.homecu.net/hcuadm/getcustatus'),
74  // array("www4", 'https://nobody:no1home@www4.homecu.net/hcuadm/getcustatus'), //development
75  $getcustatusvalues,
76  ) as $locations){
77 
78  list($server, $url) = $locations;
79  //$resp = json_decode(file_get_contents($url),true); //request status and decode json in to array
80  $ch = curl_init();
81  curl_setopt($ch, CURLOPT_URL, $url);
82  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
83  $output = curl_exec($ch);
84  $resp = json_decode($output, true);
85  curl_close($ch);
86  // $env['logger']->warning("getcustatus response " . json_encode($resp));
87 
88  foreach( $resp as $name => $values ){
89  if ($env['devmode']) {
90  $env['logger']->warning("cuname " . $name . " values " . json_encode($values));
91  }
92  if(HCU_array_key_exists($name, $res)){ //if there is an entry for this cu
93  if( $res[$name]["www_server"] == $server){ //and the server name must match the "www_server" of the $res[$name]
94  foreach( array("livebatch", "offlinestat") as $field ) {
95  $res[$name][$field] = $values[$field]; //add the status values to the cu entry
96  };
97  };
98  };
99  };
100 };
101 
102 header('Content-Type: application/json');
103 echo json_encode($res); //output everything
104 
105 /* sample output
106 {
107  cruisecu: {
108  name: "cruisecu",
109  cuname: "Cruise DC Development",
110  vendor: "CRUISE",
111  lat: "",
112  lng: "",
113  assets: "0.0",
114  members: "0",
115  www_server: "stage.homecu.io",
116  livebatch: "L",
117  offlinestat: ""
118  },
119  scrubcu: {
120  name: "scrubcu",
121  cuname: "Scrubs Live",
122  vendor: "CRUISE",
123  lat: "",
124  lng: "",
125  assets: "1.0",
126  members: "14",
127  www_server: "stage.homecu.io",
128  livebatch: "B",
129  offlinestat: "N"
130  }
131 }
132 */
133 
134 ?>
135