function openqrm_nagios3_appliance($cmd, $appliance_fields)
{
    global $event;
    global $OPENQRM_SERVER_BASE_DIR;
    global $OPENQRM_SERVER_IP_ADDRESS;
    global $OPENQRM_EXEC_PORT;
    $appliance_id = $appliance_fields["appliance_id"];
    $appliance = new appliance();
    $appliance->get_instance_by_id($appliance_id);
    $appliance_name = $appliance_fields["appliance_name"];
    $resource = new resource();
    $resource->get_instance_by_id($appliance_fields["appliance_resources"]);
    $appliance_ip = $resource->ip;
    // check appliance values, maybe we are in update and they are incomplete
    if ($appliance->imageid == 1) {
        return;
    }
    if ($resource->id == "-1" || $resource->id == "") {
        return;
    }
    // get the nagios service checks
    $nagios_host = new nagios3_host();
    $nagios_host->get_instance_by_appliance_id($appliance_id);
    $active_nagios_services = explode(',', $nagios_host->appliance_services);
    $nagios_service_list = '';
    foreach ($active_nagios_services as $service_id) {
        $nagios_service = new nagios3_service();
        $nagios_service->get_instance_by_id($service_id);
        $nagios_service_list = $nagios_service_list . "," . $nagios_service->port;
    }
    $nagios_service_list = substr($nagios_service_list, 1);
    if (!strlen($nagios_service_list)) {
        $event->log("openqrm_new_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-nagios3-appliance-hook.php", "Appliance {$appliance_id} has no configured nagios services, skipping...", "", "", 0, 0, $appliance_id);
        return 0;
    }
    $event->log("openqrm_new_appliance", $_SERVER['REQUEST_TIME'], 5, "openqrm-nagios3-appliance-hook.php", "Handling {$cmd} event {$appliance_id}/{$appliance_name}/{$appliance_ip}", "", "", 0, 0, $appliance_id);
    switch ($cmd) {
        case "start":
            $nagios_appliance_start_cmd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/nagios3/bin/openqrm-nagios-manager add -n " . $appliance_name . " -i " . $resource->ip . " -p " . $nagios_service_list . " --openqrm-cmd-mode background";
            $openqrm_server = new openqrm_server();
            $openqrm_server->send_command($nagios_appliance_start_cmd, NULL, true);
            break;
        case "stop":
            $nagios_appliance_stop_cmd = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/nagios3/bin/openqrm-nagios-manager remove_host -n " . $appliance_name . " --openqrm-cmd-mode background";
            $openqrm_server = new openqrm_server();
            $openqrm_server->send_command($nagios_appliance_stop_cmd, NULL, true);
            break;
        case "remove":
            $nagios_appliance_stop_cmd = "{$OPENQRM_SERVER_BASE_DIR}/openqrm/plugins/nagios3/bin/openqrm-nagios-manager remove_host -n " . $appliance_name . " --openqrm-cmd-mode background";
            $openqrm_server = new openqrm_server();
            $openqrm_server->send_command($nagios_appliance_stop_cmd, NULL, true);
            // remove nagios_host from the db
            $nagios_host->remove_by_appliance_id($appliance_id);
            break;
    }
}
function openqrm_hybrid_cloud_monitor()
{
    global $event;
    global $OPENQRM_SERVER_BASE_DIR;
    global $OPENQRM_SERVER_IP_ADDRESS;
    global $OPENQRM_EXEC_PORT;
    global $openqrm_server;
    global $BaseDir;
    global $RootDir;
    $now = $_SERVER['REQUEST_TIME'];
    // $event->log("hybrid_cloud_monitor", $_SERVER['REQUEST_TIME'], 2, "hybrid-cloud-monitor-hook", "Hybrid Cloud monitor hook DISABLED for now!!!", "", "", 0, 0, 0);
    // return;
    // $event->log("hybrid_cloud_monitor", $_SERVER['REQUEST_TIME'], 5, "hybrid-cloud-monitor-hook", "Hybrid Cloud monitor hook", "", "", 0, 0, 0);
    $last_stats = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/hybrid-cloud/web/hybrid-cloud-stat/last_statistics";
    if (file_exists($last_stats)) {
        $last_host_stats = file_get_contents($last_stats);
        $secs_after_last_host_stat = $now - $last_host_stats;
        if ($secs_after_last_host_stat > 35) {
            file_put_contents($last_stats, $now);
            $server = new openqrm_server();
            $hc = new hybrid_cloud();
            $hc_account_arr = $hc->get_ids();
            foreach ($hc_account_arr as $id) {
                $hc_account_id = $id['hybrid_cloud_id'];
                $hc->get_instance_by_id($hc_account_id);
                $hc_authentication = '';
                // for every ec2/euca cloud account monitor every configured region
                if ($hc->account_type == 'aws' || $hc->account_type == 'euca') {
                    $hybrid_cloud_conf = $OPENQRM_SERVER_BASE_DIR . '/openqrm/plugins/hybrid-cloud/etc/openqrm-plugin-hybrid-cloud.conf';
                    $hybrid_cloud_conf_arr = openqrm_parse_conf($hybrid_cloud_conf);
                    $region_arr = explode(",", $hybrid_cloud_conf_arr['OPENQRM_PLUGIN_HYBRID_CLOUD_REGIONS']);
                    $hc_authentication .= ' -O ' . $hc->access_key;
                    $hc_authentication .= ' -W ' . $hc->secret_key;
                }
                // one region for openstack
                if ($hc->account_type == 'lc-openstack') {
                    $region_arr = array("OpenStack");
                    $hc_authentication .= ' -u ' . $hc->username;
                    $hc_authentication .= ' -p ' . $hc->password;
                    $hc_authentication .= ' -q ' . $hc->host;
                    $hc_authentication .= ' -x ' . $hc->port;
                    $hc_authentication .= ' -g ' . $hc->tenant;
                    $hc_authentication .= ' -e ' . $hc->endpoint;
                }
                foreach ($region_arr as $region) {
                    $event->log("hybrid_cloud_monitor", $_SERVER['REQUEST_TIME'], 5, "hybrid-cloud-monitor-hook", "Hybrid Cloud monitor - checking Cloud statistics for Account " . $hc->account_name . " - " . $region, "", "", 0, 0, 0);
                    $statfile = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/hybrid-cloud/web/hybrid-cloud-stat/" . $hc_account_id . ".instances_statistics.log";
                    $command = $OPENQRM_SERVER_BASE_DIR . '/openqrm/plugins/hybrid-cloud/bin/openqrm-hybrid-cloud-vm describe --statistics true';
                    $command .= ' -i ' . $hc->id;
                    $command .= ' -n ' . $hc->account_name;
                    $command .= ' -t ' . $hc->account_type;
                    $command .= ' -ir ' . $region;
                    $command .= $hc_authentication;
                    $command .= ' --openqrm-cmd-mode background';
                    if (file_exists($statfile)) {
                        unlink($statfile);
                    }
                    $server->send_command($command, NULL, true);
                    while (!file_exists($statfile)) {
                        usleep(10000);
                        clearstatcache();
                    }
                    $content = file_get_contents($statfile);
                    $content = explode("\n", $content);
                    $b = array();
                    foreach ($content as $k => $v) {
                        if ($v !== '') {
                            $tmp = explode('@', $v);
                            $name = $tmp[1];
                            $ami = $tmp[2];
                            $public_hostname = $tmp[3];
                            $private_hostname = $tmp[4];
                            $state = $tmp[5];
                            $keypair = $tmp[6];
                            $unknown1 = $tmp[7];
                            $unknown2 = $tmp[8];
                            $type = $tmp[9];
                            $date = $tmp[10];
                            $region = $tmp[11];
                            $unknown4 = $tmp[12];
                            $unknown5 = $tmp[13];
                            $unknown6 = $tmp[14];
                            $monitoring = $tmp[15];
                            $public_ip = $hc->format_ip_address($tmp[16]);
                            $private_ip = $hc->format_ip_address($tmp[17]);
                            $unknown7 = $tmp[18];
                            $unknown8 = $tmp[19];
                            $store = $tmp[20];
                            $unknown9 = $tmp[21];
                            $unknown10 = $tmp[22];
                            $unknown11 = $tmp[23];
                            $unknown12 = $tmp[24];
                            $hvm = $tmp[25];
                            $virt_type = $tmp[26];
                            $mac = '';
                            // check for idle instances
                            $resource = new resource();
                            if ($state == 'idle') {
                                $mac = $tmp[30];
                                $resource->get_instance_by_mac($mac);
                                $resource_fields["resource_state"] = 'active';
                                $resource_fields["resource_lastgood"] = $now;
                                $resource_fields["resource_cpunumber"] = $hc->translate_resource_components('cpu', $type);
                                $resource_fields["resource_nics"] = $hc->translate_resource_components('net', $type);
                                $resource_fields["resource_memtotal"] = $hc->translate_resource_components('mem', $type);
                                $resource_fields["resource_memused"] = "0";
                                $resource_fields["resource_load"] = "0";
                                // restore mgmt ip
                                $resource_fields["resource_ip"] = $resource->network;
                                $resource->update_info($resource->id, $resource_fields);
                            } else {
                                if ($state == 'running') {
                                    // check if existing, if not auto-create resource, image and appliance
                                    if ($resource->exists_by_name($name)) {
                                        // update stats
                                        $resource->get_instance_id_by_hostname($name);
                                        $resource->get_instance_by_id($resource->id);
                                        $resource_fields["resource_state"] = 'active';
                                        $resource_fields["resource_lastgood"] = $now;
                                        $resource_fields["resource_cpunumber"] = $hc->translate_resource_components('cpu', $type);
                                        $resource_fields["resource_nics"] = $hc->translate_resource_components('net', $type);
                                        $resource_fields["resource_memtotal"] = $hc->translate_resource_components('mem', $type);
                                        $resource_fields["resource_memused"] = $resource_fields["resource_memtotal"];
                                        $resource_fields["resource_load"] = "1";
                                        if (strlen($public_ip) && $resource->ip != $public_ip) {
                                            // set public ip, update early and run nagios hook
                                            $resource_fields["resource_ip"] = $public_ip;
                                            $resource->update_info($resource->id, $resource_fields);
                                            // nagios enabled and started ?
                                            if (file_exists($RootDir . "/plugins/nagios3/.running")) {
                                                $virtualization = new virtualization();
                                                $virtualization->get_instance_by_type("hybrid-cloud-vm-local");
                                                $hc_appliance = new appliance();
                                                $hc_appliance->get_instance_by_virtualization_and_resource($virtualization->id, $resource->id);
                                                if (strlen($hc_appliance->name)) {
                                                    // special nagios classes
                                                    require_once $RootDir . "/plugins/nagios3/class/nagios3_service.class.php";
                                                    require_once $RootDir . "/plugins/nagios3/class/nagios3_host.class.php";
                                                    // get the nagios service checks
                                                    $nagios_host = new nagios3_host();
                                                    $nagios_host->get_instance_by_appliance_id($hc_appliance->id);
                                                    $active_nagios_services = explode(',', $nagios_host->appliance_services);
                                                    $nagios_service_list = '';
                                                    foreach ($active_nagios_services as $service_id) {
                                                        $nagios_service = new nagios3_service();
                                                        $nagios_service->get_instance_by_id($service_id);
                                                        $nagios_service_list = $nagios_service_list . "," . $nagios_service->port;
                                                    }
                                                    $nagios_service_list = substr($nagios_service_list, 1);
                                                    if (strlen($nagios_service_list)) {
                                                        // appliance has nagios service checks configured
                                                        $nagios_appliance_stop_cmd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/nagios3/bin/openqrm-nagios-manager remove_host -n " . $hc_appliance->name . " --openqrm-cmd-mode background";
                                                        $server->send_command($nagios_appliance_stop_cmd, NULL, true);
                                                        sleep(2);
                                                        $nagios_appliance_start_cmd = $OPENQRM_SERVER_BASE_DIR . "/openqrm/plugins/nagios3/bin/openqrm-nagios-manager add -n " . $hc_appliance->name . " -i " . $public_ip . " -p " . $nagios_service_list . " --openqrm-cmd-mode background";
                                                        $server->send_command($nagios_appliance_start_cmd, NULL, true);
                                                    }
                                                }
                                            }
                                            // nagios finished
                                        }
                                        $resource->update_info($resource->id, $resource_fields);
                                    } else {
                                        // through error for now
                                        $event->log("hybrid_cloud_monitor", $_SERVER['REQUEST_TIME'], 5, "hybrid-cloud-monitor-hook", "New Resource detected with name " . $name . "!", "", "", 0, 0, 0);
                                    }
                                }
                            }
                        }
                    }
                    unlink($statfile);
                }
            }
        }
    } else {
        file_put_contents($last_stats, $now);
    }
}
 function add()
 {
     $response = $this->get_response();
     $form = $response->form;
     if ($response->submit() && !$form->get_request('auto')) {
         if (!$form->get_request('manual_port')) {
             $form->set_error('manual_port', sprintf($response->html->lang['form']['error_required'], $this->lang['manual_port']));
         }
         if (!$form->get_request('manual_service')) {
             $form->set_error('manual_service', sprintf($response->html->lang['form']['error_required'], $this->lang['manual_service']));
         }
         if (!$form->get_request('manual_description')) {
             $form->set_error('manual_description', sprintf($response->html->lang['form']['error_required'], $this->lang['manual_description']));
         }
     }
     if (!$form->get_errors() && $response->submit()) {
         // ignore auto if manual values are set
         if ($form->get_request('manual_port') && $form->get_request('manual_service')) {
             $fields['nagios3_service_port'] = $form->get_request('manual_port');
             $fields['nagios3_service_name'] = $form->get_request('manual_service');
             if ($form->get_request('manual_type')) {
                 $fields['nagios3_service_type'] = $form->get_request('manual_type');
             } else {
                 $fields['nagios3_service_type'] = 'tcp';
             }
             if ($form->get_request('manual_description')) {
                 $fields['nagios3_service_description'] = $form->get_request('manual_description');
             }
         } else {
             if ($form->get_request('auto')) {
                 $v = explode('@', $form->get_request('auto'));
                 $fields['nagios3_service_port'] = $v[0];
                 $fields['nagios3_service_name'] = $v[1];
                 $fields['nagios3_service_type'] = $v[2];
                 $fields['nagios3_service_description'] = $v[3];
             }
         }
         // check port in use
         $nagios3 = new nagios3_service();
         $result = $nagios3->get_instance_by_port($fields['nagios3_service_port']);
         if ($result->port !== '') {
             $response->error = sprintf($this->lang['error_port_in_use'], $fields['nagios3_service_port']);
         }
         if (!isset($response->error)) {
             $fields['nagios3_service_id'] = (int) str_replace(".", "", str_pad(microtime(true), 15, "0"));
             $error = $nagios3->add($fields);
             if (!isset($error)) {
                 $response->msg = sprintf($this->lang['msg_added'], $fields['nagios3_service_name']);
             }
         }
     }
     return $response;
 }