function elinkSlot($ip)
{
    $genericSnmpPath = $_SERVER["DOCUMENT_ROOT"] . "/vanguardhe/php_scripts/oidget/genericSnmp.php";
    require_once $genericSnmpPath;
    // to initialize snmp
    $slot[0] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.1");
    $slot[1] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.2");
    $slot[2] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.3");
    $slot[3] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.4");
    $slot[4] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.5");
    $slot[5] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.6");
    $slot[6] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.7");
    $slot[7] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.8");
    $slot[8] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.9");
    $slot[9] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.10");
    $slot[10] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.11");
    $slot[11] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.12");
    $slot[12] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.13");
    $slot[13] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.14");
    $slot[14] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.15");
    $slot[15] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.16");
    for ($i = 0; $i < 16; $i++) {
        # code...
        if (substr($slot[$i], 1, 2) != "no") {
            # code...
            switch (substr($slot[$i], 0, 4)) {
                case 'EL-E':
                    # code...
                    $deviceName[] = 'EL-EMS2';
                    $devicePos[] = $i + 1;
                    //echo $slot[$i];
                    break;
                case 'EL-R':
                    # code...
                    $deviceName[] = 'EL-RRX';
                    $devicePos[] = $i + 1;
                    //echo $slot[$i];
                    break;
                case 'EL-F':
                    # code...
                    $deviceName[] = 'EL-FTX';
                    $devicePos[] = $i + 1;
                    //echo $slot[$i];
                    break;
                case 'EL-P':
                    // # code...
                    $deviceName[] = 'EL-PS';
                    $devicePos[] = $i + 1;
                    //echo $slot[$i];
                    break;
            }
        }
    }
    return array($deviceName, $devicePos);
}
function get_1550($ip)
{
    $value[0] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.1.1");
    $value[1] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.2.1");
    $value[2] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.3.1");
    $value[3] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.4.1");
    $value[4] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.5.1");
    $value[5] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.6.1");
    $value[6] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.7.1");
    $value[7] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.8.1");
    $value[8] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.9.1");
    $value[9] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.10.1");
    $value[10] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.11.1");
    $value[11] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.12.1");
    $value[12] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.13.1");
    $value[13] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.14.1");
    $value[14] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.15.1");
    $value[15] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.16.1");
    $value[16] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.17.1");
    $value[17] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.18.1");
    $value[18] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.19.1");
    $value[19] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.20.1");
    $lab[0] = "Status Index";
    $lab[1] = "IDcode";
    $lab[2] = "sub-ID";
    $lab[3] = "Firmware Version";
    $lab[4] = "Laser IM";
    $lab[5] = "Laser Temperature (C)";
    $lab[6] = "Laser Bias";
    $lab[7] = "RF Modulation Level";
    $lab[8] = "DC24V Voltage";
    $lab[9] = "DC12V Voltage";
    $lab[10] = "DC5V Voltage";
    $lab[11] = "-5VDC Voltage";
    $lab[12] = "Tx Optical Power";
    $lab[13] = "Gain Control Setting";
    $lab[14] = "SBS CONTROL Setting";
    $lab[15] = "CTB_CONTROL_Setting";
    $lab[16] = "Tx RF Module Level";
    $lab[17] = "Present AC Power 1 status";
    $lab[18] = "Present AC Power 2 status";
    $lab[19] = "Tx AC Power supply status";
    $defaultIp = snmpget_smallp($ip, ".1.3.6.1.4.1.33826.3.1.1.0");
    $defaultMac = snmpget_smallp($ip, ".1.3.6.1.4.1.33826.3.1.5.0");
    /////////////////////////////////////////////// for asset values
    $sysDescr = snmpget_smallp($ip, ".1.3.6.1.2.1.1.1.0");
    $sysObjectID = snmpget_smallp($ip, ".1.3.6.1.2.1.1.2.0");
    $sysUpTime = snmpget_smallp($ip, ".1.3.6.1.2.1.1.3.0");
    $sysContact = snmpget_smallp($ip, ".1.3.6.1.2.1.1.4.0");
    $sysName = snmpget_smallp($ip, ".1.3.6.1.2.1.1.5.0");
    $sysLocation = snmpget_smallp($ip, ".1.3.6.1.2.1.1.6.0");
    $sysService = snmpget_smallp($ip, ".1.3.6.1.2.1.1.7.0");
    // for testing purpose
    echo "EG 1550 Transmitter <br>\n\t";
    echo "\n\t<style>\n\n\ntable {\n    width:100%;\n}\n\ntable, th , td {\n    border: 1px solid grey;\n    border-collapse: collapse;\n    padding: 5px;\n}\n\nth {\n    text-align: left; \n}\n\ntable tr:nth-child(odd) {\n    background-color: #f1f1f1;\n}\ntable tr:nth-child(even) {\n    background-color: #ffffff;\n}\n\n\n\n\n\n\n\n\n\n\t</style>\n\t";
    echo "<table style=width:50%>";
    echo "<tr>";
    echo "<td>Description</td>";
    echo "<td>" . $sysDescr . "</td>";
    echo "</tr><tr>";
    echo "<td>ObjectID</td>";
    echo "<td>" . $sysObjectID . "</td>";
    echo "</tr><tr>";
    echo "<td>IP</td>";
    echo "<td>" . $defaultIp . "</td>";
    echo "</tr><tr>";
    echo "<td>MAC</td>";
    echo "<td>" . $defaultMac . "</td>";
    echo "</tr><tr>";
    echo "<td>Up Time</td>";
    echo "<td>" . $sysUpTime . "</td>";
    echo "</tr><tr>";
    echo "<td>Contact</td>";
    echo "<td>" . $sysContact . "</td>";
    echo "</tr><tr>";
    echo "<td>Name</td>";
    echo "<td>" . $sysName . "</td>";
    echo "</tr><tr>";
    echo "<td>Location</td>";
    echo "<td>" . $sysLocation . "</td>";
    echo "</tr><tr>";
    echo "<td>Service</td>";
    echo "<td>" . $sysService . "</td>";
    echo "</tr>";
    for ($i = 0; $i < 20; $i++) {
        echo "<tr>";
        echo "<td>" . $lab[$i] . "</td>";
        echo "<td>" . $value[$i] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
}
function get_elink($ip)
{
    require "genericSnmp.php";
    // for summary table
    $device[0] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.1");
    $device[1] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.4");
    $device[2] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.6");
    $device[3] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.8");
    $device[4] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.11");
    $device[5] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.14");
    $device[6] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.16");
    $sn[0] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4.1");
    $sn[1] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4.4");
    $sn[2] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4.6");
    $sn[3] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4.8");
    $sn[4] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4.11");
    $sn[5] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4.14");
    $sn[6] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4.16");
    $temp[0] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2.1.0");
    $temp[1] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2.4.0");
    $temp[2] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2.6.0");
    $temp[3] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2.8.0");
    $temp[4] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2.11.0");
    $temp[5] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2.14.0");
    $temp[6] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2.16.0");
    $temp[0] = rtrim($temp[0], "0");
    $temp[1] = rtrim($temp[1], "0");
    $temp[2] = rtrim($temp[2], "0");
    $temp[3] = rtrim($temp[3], "0");
    $temp[4] = rtrim($temp[4], "0");
    $temp[5] = rtrim($temp[5], "0");
    $temp[6] = rtrim($temp[6], "0");
    $sysDescr = snmpget_smallp($ip, ".1.3.6.1.2.1.1.1.0");
    $sysObjectID = snmpget_smallp($ip, ".1.3.6.1.2.1.1.2.0");
    $sysUpTime = snmpget_smallp($ip, ".1.3.6.1.2.1.1.3.0");
    $sysContact = snmpget_smallp($ip, ".1.3.6.1.2.1.1.4.0");
    $sysName = snmpget_smallp($ip, ".1.3.6.1.2.1.1.5.0");
    $sysLocation = snmpget_smallp($ip, ".1.3.6.1.2.1.1.6.0");
    $sysService = snmpget_smallp($ip, ".1.3.6.1.2.1.1.7.0");
    $alarm = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.2.4.0");
    $ipadd = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.9.0");
    // -------------------------------------------------------------------------------------------
    // for power supply status
    $ps[0] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.2.1.1.1.1.3.16.0");
    $ps[1] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.2.1.1.2.1.2.16.0");
    $ps[2] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.2.1.1.2.1.3.16.0");
    $ps[3] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.2.1.1.2.1.4.16.0");
    $psl[0] = "Input (V)";
    $psl[1] = "Output Voltage (V)";
    $psl[2] = "Output Current (mA)";
    $psl[3] = "Output Power (W)";
    // for NMS status
    $nms[0] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.5.0");
    $nms[1] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.8.0");
    $nms[2] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.9.0");
    $nms[3] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.10.0");
    $nms[4] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.12.0");
    $nms[5] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.13.0");
    $nms[6] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.17.0");
    $nmsl[0] = "Vendor";
    $nmsl[1] = "Alarm Detection";
    $nmsl[2] = "IP";
    $nmsl[3] = "Check Code";
    $nmsl[4] = "Tamper Status";
    $nmsl[5] = "Internal Temperature";
    $nmsl[6] = "Craft Status";
    // for receiver status
    // input
    $ri[0] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2.4.1");
    $ri[1] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2.4.2");
    $ri[2] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2.4.3");
    $ri[3] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2.4.4");
    $ri[4] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2.6.1");
    $ri[5] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2.6.2");
    $ri[6] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2.6.3");
    $ri[7] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2.6.4");
    $ri[8] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2.14.1");
    $ri[9] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2.14.2");
    $ri[10] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2.14.3");
    $ri[11] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2.14.4");
    // status
    $rs[0] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4.4.1");
    $rs[1] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4.4.2");
    $rs[2] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4.4.3");
    $rs[3] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4.4.4");
    $rs[4] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4.6.1");
    $rs[5] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4.6.2");
    $rs[6] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4.6.3");
    $rs[7] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4.6.4");
    $rs[8] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4.14.1");
    $rs[9] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4.14.2");
    $rs[10] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4.14.3");
    $rs[11] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4.14.4");
    // for transmiter status
    $t[0] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.2.1.2.8.1");
    $t[1] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.2.1.2.11.1");
    $tl[0] = "RF Input Power 1 (dBm)";
    $tl[1] = "RF Input Power 2 (dBm)";
    $t[2] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.2.1.4.8.0");
    $t[3] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.2.1.4.11.0");
    $tl[2] = "Automatic Gain Control Mode 1";
    $tl[3] = "Automatic Gain Control Mode 2";
    $t[4] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.2.8.0");
    $t[5] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.2.11.0");
    $tl[4] = "Transmitter Laser Temperature 1 (C)";
    $tl[5] = "Transmitter Laser Temperature 2 (C)";
    $t[6] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.3.8.0");
    $t[7] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.3.11.0");
    $tl[6] = "Transmitter Laser Bias Current 1 (mA)";
    $tl[7] = "Transmitter Laser Bias Current 2 (mA)";
    $t[8] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.4.8.0");
    $t[9] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.4.11.0");
    $tl[8] = "Laser Output Power 1 (mW)";
    $tl[9] = "Laser Output Power 2 (mW)";
    $t[10] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.5.8.0");
    $t[11] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.5.11.0");
    $tl[10] = "Laser Thermo Electric Cooler Current 1 (mA)";
    $tl[11] = "Laser Thermo Electric Cooler Current 2 (mA)";
    $t[12] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.6.8.0");
    $t[13] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.6.11.0");
    $tl[12] = "Laser Type 1";
    $tl[13] = "Laser Type 2";
    $t[14] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.7.8.0");
    $t[15] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.7.11.0");
    $tl[14] = "Laser Wavelength 1 (nm)";
    $tl[15] = "Laser Wavelength 2 (nm)";
    // output as a table
    echo "<style>\n\ttable {\n\t\twidth:100%;\n\t}\n\ttable, th , td {\n\t\tborder: 1px solid grey;\n\t\tborder-collapse: collapse;\n\t\tpadding: 5px;\n\t}\n\tth {\n\t\ttext-align: left; \n\t}\n\ttable tr:nth-child(odd) {\n\t\tbackground-color: #f1f1f1;\n\t}\n\ttable tr:nth-child(even) {\n\t\tbackground-color: #ffffff;\n\t}</style>";
    // --------------------------------------
    echo "ELink Optical Headend Platform Summary<br>";
    echo "<table style=width:50%>";
    echo "<tr>";
    echo "<td>System</td>";
    echo "<td>" . $sysDescr . "</td>";
    echo "</tr><tr>";
    echo "<td>IP</td>";
    echo "<td>" . $ipadd . "</td>";
    echo "</tr><tr>";
    echo "<td>Up Time</td>";
    echo "<td>" . $sysUpTime . "</td>";
    echo "</tr><tr>";
    echo "<td>Contact</td>";
    echo "<td>" . $sysContact . "</td>";
    echo "</tr><tr>";
    echo "<td>Name</td>";
    echo "<td>" . $sysName . "</td>";
    echo "</tr><tr>";
    echo "<td>Location</td>";
    echo "<td>" . $sysLocation . "</td>";
    echo "</tr><tr>";
    echo "<td>Service</td>";
    echo "<td>" . $sysService . "</td>";
    echo "</tr>";
    echo "<td>Current Alarm</td>";
    echo "<td>" . $alarm . "</td>";
    echo "</tr>";
    for ($i = 0; $i < 7; $i++) {
        echo "<tr>";
        echo "<td>" . $device[$i] . "</td>";
        echo "<td>" . "Serial Number: " . $sn[$i] . "</td>";
        echo "</tr>";
    }
    // (Temperature: " . $temp[$i] . " C)"
    echo "</table>";
    echo "<br>";
    //----------------------------------------
    echo "Power Supply Status<br>";
    echo "<table style=width:50%>";
    for ($i = 0; $i < 4; $i++) {
        echo "<tr>";
        echo "<td>" . $psl[$i] . "</td>";
        echo "<td>" . $ps[$i] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    echo "<br>";
    //------------------------------------
    echo "Network Management Status<br>";
    echo "<table style=width:50%>";
    for ($i = 0; $i < 7; $i++) {
        echo "<tr>";
        echo "<td>" . $nmsl[$i] . "</td>";
        echo "<td>" . $nms[$i] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    echo "<br>";
    //------------------------------------
    echo "Optical Receiver Status<br>";
    echo "<table style=width:50%>";
    echo "<tr>";
    echo "<td>Input Power (RX1) (V)</td>";
    echo "<td>" . "1: " . $ri[0] . "; 2: " . $ri[1] . "; 3: " . $ri[2] . "; 4: " . $ri[3] . "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Input Power (RX2) (V)</td>";
    echo "<td>" . "1: " . $ri[4] . "; 2: " . $ri[5] . "; 3: " . $ri[6] . "; 4: " . $ri[7] . "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Input Power (RX3) (V)</td>";
    echo "<td>" . "1: " . $ri[8] . "; 2: " . $ri[9] . "; 3: " . $ri[10] . "; 4: " . $ri[11] . "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Status (RX1)</td>";
    echo "<td>" . "1: " . $rs[0] . "; 2: " . $rs[1] . "; 3: " . $rs[2] . "; 4: " . $rs[3] . "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Status (RX2)</td>";
    echo "<td>" . "1: " . $rs[4] . "; 2: " . $rs[5] . "; 3: " . $rs[6] . "; 4: " . $rs[7] . "</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Status (RX3)</td>";
    echo "<td>" . "1: " . $rs[8] . "; 2: " . $rs[9] . "; 3: " . $rs[10] . "; 4: " . $rs[11] . "</td>";
    echo "</tr>";
    echo "</table>";
    echo "<br>";
    //------------------------------------
    echo "Optical Transmitter Status<br>";
    echo "<table style=width:50%>";
    for ($i = 0; $i < 16; $i++) {
        echo "<tr>";
        echo "<td>" . $tl[$i] . "</td>";
        echo "<td>" . $t[$i] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    echo "<br>";
}
function daemon_snmpScanIntoDb_1550($ip)
{
    // TO-DO: 1) extract all snmp value from 1550, 2) check if the table is existed, 3) if not existed, create it, and 4) put it into db. This action should be wrapped in a function.
    // header for initialization
    $genericSnmpPath = $_SERVER["DOCUMENT_ROOT"] . "/vanguardhe/php_scripts/oidget/genericSnmp.php";
    require_once $genericSnmpPath;
    // to initialize snmp
    require "daemon_db_init.php";
    // to initialize database connection
    require_once "daemon_getDeviceIdPerIp.php";
    // to initialize database connection
    // 1, extract all snmp value from 1550
    //$timestamp = "'" . date('YmdGis') . "'";
    $deviceid = getDeviceIdPerIp($ip);
    //echo $deivceid;
    global $timestamp;
    $recordedIp = trim(deco_1550($ip));
    $sysDescr = deco_1550(snmpget_smallp($ip, ".1.3.6.1.2.1.1.1.0"));
    $sysObjectID = deco_1550(snmpget_smallp($ip, ".1.3.6.1.2.1.1.2.0"));
    $sysUpTime = deco_1550(snmpget_smallp($ip, ".1.3.6.1.2.1.1.3.0"));
    $sysContact = deco_1550(snmpget_smallp($ip, ".1.3.6.1.2.1.1.4.0"));
    $sysName = deco_1550(snmpget_smallp($ip, ".1.3.6.1.2.1.1.5.0"));
    $sysLocation = deco_1550(snmpget_smallp($ip, ".1.3.6.1.2.1.1.6.0"));
    $sysService = deco_1550(snmpget_smallp($ip, ".1.3.6.1.2.1.1.7.0"));
    $defaultIp = trim(deco_1550(snmpget_smallp($ip, ".1.3.6.1.4.1.33826.3.1.1.0")));
    $defaultMac = deco_1550(snmpget_smallp($ip, ".1.3.6.1.4.1.33826.3.1.5.0"));
    $value[0] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.1.1");
    $value[1] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.2.1");
    $value[2] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.3.1");
    $value[3] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.4.1");
    $value[4] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.5.1");
    $value[5] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.6.1");
    $value[6] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.7.1");
    $value[7] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.8.1");
    $value[8] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.9.1");
    $value[9] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.10.1");
    $value[10] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.11.1");
    $value[11] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.12.1");
    $value[12] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.13.1");
    $value[13] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.14.1");
    $value[14] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.15.1");
    $value[15] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.16.1");
    $value[16] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.17.1");
    $value[17] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.18.1");
    $value[18] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.19.1");
    $value[19] = snmpget_smallp($ip, ".1.3.6.1.4.1.3222.4.6.1.1.20.1");
    for ($i = 0; $i < 20; $i++) {
        # code...
        $value[$i] = deco_1550($value[$i]);
    }
    $lab[0] = "StatusIndex";
    $lab[1] = "IDcode";
    $lab[2] = "subID";
    $lab[3] = "FirmwareVersion";
    $lab[4] = "Laser IM";
    $lab[5] = "Laser Temperature (C)";
    $lab[6] = "Laser Bias";
    $lab[7] = "RF Modulation Level";
    $lab[8] = "DC24V Voltage";
    $lab[9] = "DC12V Voltage";
    $lab[10] = "DC5V Voltage";
    $lab[11] = "-5VDC Voltage";
    $lab[12] = "Tx Optical Power";
    $lab[13] = "Gain Control Setting";
    $lab[14] = "SBS CONTROL Setting";
    $lab[15] = "CTB_CONTROL_Setting";
    $lab[16] = "Tx RF Module Level";
    $lab[17] = "Present AC Power 1 status";
    $lab[18] = "Present AC Power 2 status";
    $lab[19] = "Tx AC Power supply status";
    // 2, check if the table "dameonSnmp1550Value" in the database "vanguardhe"
    $query_exist = "SELECT relname FROM pg_class \n\tWHERE relname = 'dameonsnmp1550value';";
    $result_exist = pg_query($query_exist) or die('Query failed: ' . pg_last_error());
    $exist = '';
    while ($row_exist = pg_fetch_object($result_exist)) {
        $exist = $row_exist->relname;
    }
    // // 3, if not existed, create it
    if ($exist != "dameonsnmp1550value") {
        # code...
        $query_construct = "CREATE TABLE PUBLIC.dameonsnmp1550value(\n\t\t\tdeviceid int,\n\t\t\ttime           TEXT    ,\n\t\t\trecordip\tTEXT,\n\t\t\tdescription            TEXT  ,\n\t\t\toids       TEXT,\n\t\t\tuptime         TEXT,\n\t\t\tcontact         TEXT,\n\t\t\tname        TEXT,\n\t\t\tlocation         TEXT,\n\t\t\tservice        TEXT,\n\t\t\tip         TEXT,\n\t\t\tmac         MACADDR,\n\t\t\tstatusindex            TEXT  ,\n\t\t\tidcode       TEXT,\n\t\t\tsubID         TEXT,\n\t\t\tFirmwareVersion         TEXT,\n\t\t\tLaserIM        TEXT,\n\t\t\tLaserTemperature        TEXT,\n\t\t\tLaserBias        TEXT,\n\t\t\tRFModulationLevel         TEXT,\n\t\t\tDC24VVoltage         TEXT,\n\t\t\tDC12VVoltage            TEXT  ,\n\t\t\tDC5VVoltage      TEXT,\n\t\t\tminor5VDCVoltage         TEXT,\n\t\t\tTxOpticalPower         TEXT,\n\t\t\tGainControlSetting        TEXT,\n\t\t\tSBSCONTROLSetting        TEXT,\n\t\t\tCTBCONTROLSetting       TEXT,\n\t\t\tTxRFModuleLevel         TEXT,\n\t\t\tPresentACPower1status        TEXT,\n\t\t\tPresentACPower2status         TEXT,\n\t\t\tTxACPowersupplystatus       TEXT\t\t\t);";
        $result_construct = pg_query($query_construct) or die('Query failed: ' . pg_last_error());
        pg_free_result($result_construct);
    }
    // 4, insert data into the table
    $query_insert = "INSERT INTO PUBLIC.dameonsnmp1550value VALUES ({$deviceid}, {$timestamp}, {$recordedIp}, {$sysDescr}, {$sysObjectID}, {$sysUpTime}, {$sysContact}, {$sysName}, {$sysLocation}, {$sysService}, {$defaultIp}, {$defaultMac}, {$value['0']}, {$value['1']}, {$value['2']}, {$value['3']}, {$value['4']}, {$value['5']}, {$value['6']}, {$value['7']}, {$value['8']}, {$value['9']}, {$value['10']}, {$value['11']}, {$value['12']}, {$value['13']}, {$value['14']}, {$value['15']}, {$value['16']}, {$value['17']}, {$value['18']}, {$value['19']});";
    $result_insert = pg_query($query_insert) or die('Query failed: ' . pg_last_error());
    // 5, check if the table "dameonSnmp1550Summary" in the database "vanguardhe": this table is for the other maybe info further
    $query_exist_sum = "SELECT relname FROM pg_class \n\tWHERE relname = 'dameonsnmp1550summary';";
    $result_exist_sum = pg_query($query_exist_sum) or die('Query failed: ' . pg_last_error());
    $exist_sum = '';
    while ($row_exist_sum = pg_fetch_object($result_exist_sum)) {
        $exist_sum = $row_exist_sum->relname;
    }
    // // 3, if not existed, create it
    if ($exist_sum != "dameonsnmp1550summary") {
        # code...
        $query_construct_sum = "CREATE TABLE PUBLIC.dameonsnmp1550summary(\n\t\t\tdescription\tText,\n\t\t\tcomments           TEXT  \t\t);";
        $result_construct_sum = pg_query($query_construct_sum) or die('Query failed: ' . pg_last_error());
        pg_free_result($result_construct_sum);
    }
    pg_free_result($result_exist);
    pg_free_result($result_insert);
    pg_free_result($result_exist_sum);
    pg_close($dbconn);
}
function addDevice($ip, $comm, $mac, $sn, $lati, $long)
{
    $genericSnmpPath = $_SERVER["DOCUMENT_ROOT"] . "/vanguardhe/php_scripts/oidget/genericSnmp.php";
    require_once $genericSnmpPath;
    // to initialize snmp
    $deco_ip = "'" . $ip . "'";
    if ($mac == "") {
        # code...
        $mac = "00:00:00:00:00:00";
    }
    if ($sn == "") {
        # code...
        $sn = "NA";
    }
    if ($lati == "") {
        # code...
        $lati = "-73.6602109";
    }
    if ($long == "") {
        # code...
        $long = "45.5263402";
    }
    $deco_mac = "'" . $mac . "'";
    $deco_sn = "'" . $sn . "'";
    $timestamp = "'" . date('YmdGis') . "'";
    $status = "'" . "online" . "'";
    if ($comm == "public") {
        # code...
        $sysDescr = "'" . snmpget_smallp($ip, ".1.3.6.1.2.1.1.1.0") . "'";
        $sysObjectID = "'" . snmpget_smallp($ip, ".1.3.6.1.2.1.1.2.0") . "'";
        $sysUpTime = "'" . snmpget_smallp($ip, ".1.3.6.1.2.1.1.3.0") . "'";
        $sysContact = "'" . snmpget_smallp($ip, ".1.3.6.1.2.1.1.4.0") . "'";
        $sysName = "'" . snmpget_smallp($ip, ".1.3.6.1.2.1.1.5.0") . "'";
        $sysLocation = "'" . snmpget_smallp($ip, ".1.3.6.1.2.1.1.6.0") . "'";
        $sysService = "'" . snmpget_smallp($ip, ".1.3.6.1.2.1.1.7.0") . "'";
    }
    if ($comm == "PUBLIC") {
        # code...
        $sysDescr = "'" . snmpget_bigP($ip, ".1.3.6.1.2.1.1.1.0") . "'";
        $sysObjectID = "'" . snmpget_bigP($ip, ".1.3.6.1.2.1.1.2.0") . "'";
        $sysUpTime = "'" . snmpget_bigP($ip, ".1.3.6.1.2.1.1.3.0") . "'";
        $sysContact = "'" . snmpget_bigP($ip, ".1.3.6.1.2.1.1.4.0") . "'";
        $sysName = "'" . snmpget_bigP($ip, ".1.3.6.1.2.1.1.5.0") . "'";
        $sysLocation = "'" . snmpget_bigP($ip, ".1.3.6.1.2.1.1.6.0") . "'";
        $sysService = "'" . snmpget_bigP($ip, ".1.3.6.1.2.1.1.7.0") . "'";
    }
    if ($comm != "PUBLIC" && $comm != "public") {
        # code...
        $sysDescr = "'" . snmpget_generic($ip, $comm, ".1.3.6.1.2.1.1.1.0") . "'";
        $sysObjectID = "'" . snmpget_generic($ip, $comm, ".1.3.6.1.2.1.1.2.0") . "'";
        $sysUpTime = "'" . snmpget_generic($ip, $comm, ".1.3.6.1.2.1.1.3.0") . "'";
        $sysContact = "'" . snmpget_generic($ip, $comm, ".1.3.6.1.2.1.1.4.0") . "'";
        $sysName = "'" . snmpget_generic($ip, $comm, ".1.3.6.1.2.1.1.5.0") . "'";
        $sysLocation = "'" . snmpget_generic($ip, $comm, ".1.3.6.1.2.1.1.6.0") . "'";
        $sysService = "'" . snmpget_generic($ip, $comm, ".1.3.6.1.2.1.1.7.0") . "'";
    }
    require_once "daemon_db_init.php";
    $query_exist = "SELECT relname FROM pg_class \n\tWHERE relname = 'daemondevice';";
    $result_exist = pg_query($query_exist) or die('Query failed: ' . pg_last_error());
    $exist = '';
    while ($row_exist = pg_fetch_object($result_exist)) {
        $exist = $row_exist->relname;
    }
    // // 3, if not existed, create it
    if ($exist != "daemondevice") {
        # code...
        $query_construct = "CREATE TABLE PUBLIC.daemondevice(\n\t\t\tid SERIAL PRIMARY KEY,\n\t\t\ttime           TEXT    ,\n\t\t\tip\t\tinet,\n\t\t\tstatus   \t\tTEXT,\n\t\t\tdescription            TEXT  ,\n\t\t\tmib    TEXT,\n\t\t\tuptime       TEXT,\n\t\t\tcontact       TEXT,\n\t\t\tname         TEXT,\n\t\t\tlocation\t\tTEXT,\n\t\t\tservice   TEXT,\n\t\t\tlatitude NUMERIC,\n\t\t\tlongtitude NUMERIC,\n\t\t\tMAC macaddr,\n\t\t\tsn TEXT,\n\t\t\tprovision TEXT);";
        $result_construct = pg_query($query_construct) or die('Query failed: ' . pg_last_error());
        pg_free_result($result_construct);
    }
    $query_value = "SELECT \n\t  daemondevice.ip\n\tFROM \n\t  public.daemondevice\n\tWHERE \n\t  daemondevice.ip = {$deco_ip};";
    $result_value = pg_query($query_value) or die('Query failed: ' . pg_last_error());
    $number = pg_num_rows($result_value);
    // the number of total device eg1550
    $method = "'" . "add" . "'";
    if ($number == 0) {
        # code...
        $query_insert = "INSERT INTO PUBLIC.daemondevice (time, ip, status, description, mib, uptime, contact, name, location, service, latitude, longtitude, MAC, sn, provision) VALUES ({$timestamp}, {$deco_ip}, {$status}, {$sysDescr}, {$sysObjectID}, {$sysUpTime}, {$sysContact}, {$sysName}, {$sysLocation}, {$sysService}, {$lati}, {$long}, {$deco_mac}, {$deco_sn}, {$method});";
        $result_insert = pg_query($query_insert) or die('Query failed: ' . pg_last_error());
        pg_free_result($result_insert);
        echo $deco_ip . " has been inserted!<br>";
    }
    if ($number == 1) {
        $query_update = "UPDATE PUBLIC.daemondevice SET \n\t\t\ttime= {$timestamp}, \n\t\t\tip= {$deco_ip}, \n\t\t\tstatus = {$status},\n\t\t\tdescription = {$sysDescr},\n\t\t\tmib= {$sysObjectID},\n\t\t\tuptime = {$sysUpTime},\n\t\t\tcontact = {$sysContact},\n\t\t\tname = {$sysName},\n\t\t\tlocation= {$sysLocation},\n\t\t\tservice = {$sysService},\n\t\t\tlatitude={$lati},\n\t\t\tlongtitude= {$long},\n\t\t\tMAC = {$deco_mac},\n\t\t\tsn={$deco_sn},\n\t\t\tprovision = {$method}\n\t\t\tWHERE daemondevice.ip={$deco_ip};";
        $result_update = pg_query($query_update) or die('Query failed: ' . pg_last_error());
        pg_free_result($result_update);
        echo $deco_ip . " has been updated!<br>";
    }
    pg_free_result($result_exist);
    pg_free_result($result_value);
    //pg_close($dbconn);
}
function writeToDatabase($ip, $s)
{
    $deco_ip = "'" . $ip . "'";
    $timestamp = "'" . date('YmdGis') . "'";
    $status = "'" . "online" . "'";
    if ($s == "public") {
        # code...
        $sysDescr = "'" . snmpget_smallp($ip, ".1.3.6.1.2.1.1.1.0") . "'";
        $sysObjectID = "'" . snmpget_smallp($ip, ".1.3.6.1.2.1.1.2.0") . "'";
        $sysUpTime = "'" . snmpget_smallp($ip, ".1.3.6.1.2.1.1.3.0") . "'";
        $sysContact = "'" . snmpget_smallp($ip, ".1.3.6.1.2.1.1.4.0") . "'";
        $sysName = "'" . snmpget_smallp($ip, ".1.3.6.1.2.1.1.5.0") . "'";
        $sysLocation = "'" . snmpget_smallp($ip, ".1.3.6.1.2.1.1.6.0") . "'";
        $sysService = "'" . snmpget_smallp($ip, ".1.3.6.1.2.1.1.7.0") . "'";
    }
    if ($s == "PUBLIC") {
        # code...
        $sysDescr = "'" . snmpget_bigP($ip, ".1.3.6.1.2.1.1.1.0") . "'";
        $sysObjectID = "'" . snmpget_bigP($ip, ".1.3.6.1.2.1.1.2.0") . "'";
        $sysUpTime = "'" . snmpget_bigP($ip, ".1.3.6.1.2.1.1.3.0") . "'";
        $sysContact = "'" . snmpget_bigP($ip, ".1.3.6.1.2.1.1.4.0") . "'";
        $sysName = "'" . snmpget_bigP($ip, ".1.3.6.1.2.1.1.5.0") . "'";
        $sysLocation = "'" . snmpget_bigP($ip, ".1.3.6.1.2.1.1.6.0") . "'";
        $sysService = "'" . snmpget_bigP($ip, ".1.3.6.1.2.1.1.7.0") . "'";
    }
    require "daemon_db_init.php";
    $query_exist = "SELECT relname FROM pg_class \n\tWHERE relname = 'daemondevice';";
    $result_exist = pg_query($query_exist) or die('Query failed: ' . pg_last_error());
    $exist = '';
    while ($row_exist = pg_fetch_object($result_exist)) {
        $exist = $row_exist->relname;
    }
    // // 3, if not existed, create it
    if ($exist != "daemondevice") {
        # code...
        $query_construct = "CREATE TABLE PUBLIC.daemondevice(\n\t\t\tid SERIAL PRIMARY KEY,\n\t\t\ttime           TEXT    ,\n\t\t\tip\t\tinet,\n\t\t\tstatus   \t\tTEXT,\n\t\t\tdescription            TEXT  ,\n\t\t\tmib    TEXT,\n\t\t\tuptime       TEXT,\n\t\t\tcontact       TEXT,\n\t\t\tname         TEXT,\n\t\t\tlocation\t\tTEXT,\n\t\t\tservice   TEXT,\n\t\t\tlatitude NUMERIC,\n\t\t\tlongtitude NUMERIC,\n\t\t\tMAC macaddr,\n\t\t\tsn TEXT,\n\t\t\tprovision TEXT);";
        $result_construct = pg_query($query_construct) or die('Query failed: ' . pg_last_error());
        pg_free_result($result_construct);
    }
    $query_value = "SELECT \n\t  daemondevice.ip\n\tFROM \n\t  public.daemondevice\n\tWHERE \n\t  daemondevice.ip = {$deco_ip};";
    $result_value = pg_query($query_value) or die('Query failed: ' . pg_last_error());
    $number = pg_num_rows($result_value);
    // the number of total device eg1550
    $method = "'" . "discover" . "'";
    if ($number == 0) {
        # code...
        $query_insert = "INSERT INTO PUBLIC.daemondevice (time, ip, status, description, mib, uptime, contact, name, location, service, provision) VALUES ({$timestamp}, {$deco_ip}, {$status}, {$sysDescr}, {$sysObjectID}, {$sysUpTime}, {$sysContact}, {$sysName}, {$sysLocation}, {$sysService}, {$method});";
        $result_insert = pg_query($query_insert) or die('Query failed: ' . pg_last_error());
        pg_free_result($result_insert);
    }
    pg_free_result($result_exist);
    pg_free_result($result_value);
    pg_close($dbconn);
}
$lab[14] = "SBS CONTROL Setting";
$lab[15] = "CTB_CONTROL_Setting";
$lab[16] = "Tx RF Module Level";
$lab[17] = "Present AC Power 1 status";
$lab[18] = "Present AC Power 2 status";
$lab[19] = "Tx AC Power supply status";
$defaultIp = snmpget_smallp($ip, ".1.3.6.1.4.1.33826.3.1.1.0");
$defaultMac = snmpget_smallp($ip, ".1.3.6.1.4.1.33826.3.1.5.0");
/////////////////////////////////////////////// for asset values
$sysDescr = snmpget_smallp($ip, ".1.3.6.1.2.1.1.1.0");
$sysObjectID = snmpget_smallp($ip, ".1.3.6.1.2.1.1.2.0");
$sysUpTime = snmpget_smallp($ip, ".1.3.6.1.2.1.1.3.0");
$sysContact = snmpget_smallp($ip, ".1.3.6.1.2.1.1.4.0");
$sysName = snmpget_smallp($ip, ".1.3.6.1.2.1.1.5.0");
$sysLocation = snmpget_smallp($ip, ".1.3.6.1.2.1.1.6.0");
$sysService = snmpget_smallp($ip, ".1.3.6.1.2.1.1.7.0");
// for testing purpose
echo "EG 1550 Transmitter <br>";
echo "------------------------------------------------<br>";
echo "IP address : " . $defaultIp . "<br>";
echo "MAC : " . $defaultMac . "<br>";
for ($i = 0; $i < 20; $i++) {
    echo $lab[$i] . " : " . $value[$i] . "<br>";
}
echo "------------------------------------------------<br>";
echo $sysDescr . "<br>";
echo $sysObjectID . "<br>";
echo $sysUpTime . "<br>";
echo $sysContact . "<br>";
echo $sysName . "<br>";
echo $sysLocation . "<br>";
function get_elink($ip)
{
    require "genericSnmp.php";
    $device[0] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.1");
    $device[1] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.4");
    $device[2] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.6");
    $device[3] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.8");
    $device[4] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.11");
    $device[5] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.14");
    $device[6] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.16");
    $sn[0] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4.1");
    $sn[1] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4.4");
    $sn[2] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4.6");
    $sn[3] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4.8");
    $sn[4] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4.11");
    $sn[5] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4.14");
    $sn[6] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4.16");
    $temp[0] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2.1.0");
    $temp[1] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2.4.0");
    $temp[2] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2.6.0");
    $temp[3] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2.8.0");
    $temp[4] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2.11.0");
    $temp[5] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2.14.0");
    $temp[6] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2.16.0");
    $value[0] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.5.0");
    // need to change the oids
    $value[1] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.8.0");
    $value[2] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.9.0");
    $value[3] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.10.0");
    $value[4] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.12.0");
    $value[5] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.13.0");
    $value[6] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.17.0");
    $ipadd = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.9.0");
    $lab[0] = "Vendor";
    $lab[1] = "Alarm Detection";
    $lab[2] = "IP";
    $lab[3] = "Check Code";
    $lab[4] = "Tamper Status";
    $lab[5] = "Internal Temperature";
    $lab[6] = "Craft Status";
    $value[7] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.2.1.2.8.1");
    $value[8] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.2.1.2.8.2");
    $lab[7] = "RF Input Power 1";
    $lab[8] = "RF Input Power 2";
    $value[9] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.2.1.4.8.0");
    $value[10] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.2.1.4.11.0");
    $lab[9] = "Automatic Gain Control Mode 1";
    $lab[10] = "Automatic Gain Control Mode 2";
    $value[11] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.2.8.0");
    $value[12] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.2.11.0");
    $lab[11] = "Transmitter Laser Temperature 1";
    $lab[12] = "Transmitter Laser Temperature 2";
    $value[13] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.3.8.0");
    $value[14] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.3.11.0");
    $lab[13] = "Transmitter Laser Bias Current 1";
    $lab[14] = "Transmitter Laser Bias Current 2";
    $value[15] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.4.8.0");
    $value[16] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.4.11.0");
    $lab[15] = "Laser Output Power 1";
    $lab[16] = "Laser Output Power 2";
    $value[17] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.5.8.0");
    $value[18] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.5.11.0");
    $lab[17] = "Laser Thermo Electric Cooler Current 1";
    $lab[18] = "Laser Thermo Electric Cooler Current 2";
    $value[19] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.6.8.0");
    $value[20] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.6.11.0");
    $lab[19] = "Laser Type 1";
    $lab[20] = "Laser Type 2";
    $value[21] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.7.8.0");
    $value[22] = snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.7.11.0");
    $lab[21] = "Laser Wavelength 1";
    $lab[22] = "Laser Wavelength 2";
    /////////////////////////////////////////////// for asset values
    $sysDescr = snmpget_smallp($ip, ".1.3.6.1.2.1.1.1.0");
    $sysObjectID = snmpget_smallp($ip, ".1.3.6.1.2.1.1.2.0");
    $sysUpTime = snmpget_smallp($ip, ".1.3.6.1.2.1.1.3.0");
    $sysContact = snmpget_smallp($ip, ".1.3.6.1.2.1.1.4.0");
    $sysName = snmpget_smallp($ip, ".1.3.6.1.2.1.1.5.0");
    $sysLocation = snmpget_smallp($ip, ".1.3.6.1.2.1.1.6.0");
    $sysService = snmpget_smallp($ip, ".1.3.6.1.2.1.1.7.0");
    // for testing purpose
    echo "ELink Optical Headend Platform <br>\n";
    echo "\n\t<style>\n\n\ntable {\n    width:100%;\n}\n\ntable, th , td {\n    border: 1px solid grey;\n    border-collapse: collapse;\n    padding: 5px;\n}\n\nth {\n    text-align: left; \n}\n\ntable tr:nth-child(odd) {\n    background-color: #f1f1f1;\n}\ntable tr:nth-child(even) {\n    background-color: #ffffff;\n}\n\n\n\n\n\n\n\n\n\n\t</style>\n\t";
    echo "<table style=width:50%>";
    echo "<tr>";
    echo "<td>Description</td>";
    echo "<td>" . $sysDescr . "</td>";
    echo "</tr><tr>";
    echo "<td>ObjectID</td>";
    echo "<td>" . $sysObjectID . "</td>";
    echo "</tr><tr>";
    echo "<td>IP</td>";
    echo "<td>" . $ipadd . "</td>";
    echo "</tr><tr>";
    echo "<td>Up Time</td>";
    echo "<td>" . $sysUpTime . "</td>";
    echo "</tr><tr>";
    echo "<td>Contact</td>";
    echo "<td>" . $sysContact . "</td>";
    echo "</tr><tr>";
    echo "<td>Name</td>";
    echo "<td>" . $sysName . "</td>";
    echo "</tr><tr>";
    echo "<td>Location</td>";
    echo "<td>" . $sysLocation . "</td>";
    echo "</tr><tr>";
    echo "<td>Service</td>";
    echo "<td>" . $sysService . "</td>";
    echo "</tr>";
    for ($i = 0; $i < 7; $i++) {
        echo "<tr>";
        echo "<td>" . $device[$i] . " (" . $sn[$i] . " ) " . "</td>";
        echo "<td>" . $temp[$i] . "</td>";
        echo "</tr>";
    }
    for ($i = 0; $i < 23; $i++) {
        echo "<tr>";
        echo "<td>" . $lab[$i] . "</td>";
        echo "<td>" . $value[$i] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
}
function daemon_snmpScanIntoDb_elink($ip)
{
    $genericSnmpPath = $_SERVER["DOCUMENT_ROOT"] . "/vanguardhe/php_scripts/oidget/genericSnmp.php";
    require_once "daemon_checkElink.php";
    require_once $genericSnmpPath;
    // to initialize snmp
    require "daemon_db_init.php";
    // to initialize database connection
    require_once "daemon_getDeviceIdPerIp.php";
    // to initialize database connection
    // 1, extract all snmp value from 1550
    //$timestamp = "'" . date('YmdGis') . "'";
    $deviceid = getDeviceIdPerIp($ip);
    // get device map
    $onlineDev = elinkSlot($ip);
    // $timestamp = deco_elink(date("j F Y h:i:s A"));
    global $timestamp;
    for ($j = 0; $j < sizeof($onlineDev[0]); $j++) {
        # code...
        $dev[] = $onlineDev[0][$j];
        $devSlot[] = $onlineDev[1][$j];
    }
    // get different device array, and ps/ems always at 16/1 slot
    for ($i = 0; $i < sizeof($dev); $i++) {
        # code...
        switch ($dev[$i]) {
            case 'EL-RRX':
                # code...
                $slot_rrx[] = $devSlot[$i];
                break;
            case 'EL-FTX':
                # code...
                $slot_ftx[] = $devSlot[$i];
        }
    }
    // get EMS all of snmp value
    $pos_ems = "1";
    $sysDescr = deco_elink(snmpget_smallp($ip, ".1.3.6.1.2.1.1.1.0"));
    $sysObjectID = deco_elink(snmpget_smallp($ip, ".1.3.6.1.2.1.1.2.0"));
    $sysUpTime = deco_elink(snmpget_smallp($ip, ".1.3.6.1.2.1.1.3.0"));
    $sysContact = deco_elink(snmpget_smallp($ip, ".1.3.6.1.2.1.1.4.0"));
    $sysName = deco_elink(snmpget_smallp($ip, ".1.3.6.1.2.1.1.5.0"));
    $sysLocation = deco_elink(snmpget_smallp($ip, ".1.3.6.1.2.1.1.6.0"));
    $sysService = deco_elink(snmpget_smallp($ip, ".1.3.6.1.2.1.1.7.0"));
    $alarm = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.2.4.0"));
    $ipadd = trim(deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.9.0")));
    $ems = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.1"));
    $ems_sn = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4.1"));
    $ems_temp = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2.1.0"));
    $nms[0] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.5.0"));
    // vendro
    $nms[1] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.8.0"));
    // alarm detection
    $nms[2] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.9.0"));
    // ip
    $nms[3] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.10.0"));
    // check code
    $nms[4] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.12.0"));
    // tamper status
    $nms[5] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.13.0"));
    // internal temperarure
    $nms[6] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.17.0"));
    // craft status
    // get PS all of snmp value
    $pos_ps = "16";
    $psu = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3.16"));
    $psu_sn = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4.16"));
    $psu_temp = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2.16.0"));
    $ps[0] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.2.1.1.1.1.3.16.0"));
    // input v
    $ps[1] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.2.1.1.2.1.2.16.0"));
    // output v
    $ps[2] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.2.1.1.2.1.3.16.0"));
    // output ma
    $ps[3] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.2.1.1.2.1.4.16.0"));
    // output W
    // get FAN all of snmp value, 1 is normal, 2 is fault
    $fan[0] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.3.1.1.2.1.3.1.0"));
    $fan[1] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.3.1.1.2.1.3.2.0"));
    $fan[2] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.3.1.1.2.1.3.3.0"));
    $fan[3] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.3.1.1.2.1.3.4.0"));
    $fan[4] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.3.1.1.2.1.3.5.0"));
    $fan[5] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.3.1.1.2.1.3.6.0"));
    $fan[6] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.3.1.1.2.1.3.7.0"));
    $fan[7] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.3.1.1.2.1.3.8.0"));
    // get RRX all of snmp value
    for ($i = 0; $i < sizeof($slot_rrx); $i++) {
        # code...
        //echo $slot_rrx[$i];
        $pos_rrx[] = $slot_rrx[$i];
        $rrx_name[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3." . $slot_rrx[$i]));
        $rrx_sn[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4." . $slot_rrx[$i]));
        $rrx_temp[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2." . $slot_rrx[$i] . ".0"));
        $rrx_input1[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2." . $slot_rrx[$i] . ".1"));
        // input power dBm
        $rrx_input2[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2." . $slot_rrx[$i] . ".2"));
        $rrx_input3[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2." . $slot_rrx[$i] . ".3"));
        $rrx_input4[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2." . $slot_rrx[$i] . ".4"));
        $rrx_status1[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4." . $slot_rrx[$i] . ".1"));
        // status, 1 is normal, 2 is fault
        $rrx_status2[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4." . $slot_rrx[$i] . ".2"));
        $rrx_status3[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4." . $slot_rrx[$i] . ".3"));
        $rrx_status4[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4." . $slot_rrx[$i] . ".4"));
    }
    // get FTX all of snmp value
    for ($i = 0; $i < sizeof($slot_ftx); $i++) {
        //echo $slot_ftx[$i];
        $pos_ftx[] = $slot_ftx[$i];
        $ftx_name[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.3." . $slot_ftx[$i]));
        $ftx_sn[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.3.1.4." . $slot_ftx[$i]));
        $ftx_temp[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2." . $slot_ftx[$i] . ".0"));
        $ftx_rfinputpower[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.2.1.2." . $slot_ftx[$i] . ".1"));
        // input power dBmV
        $ftx_agcmode[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.2.1.4." . $slot_ftx[$i] . ".0"));
        // AGC MODE   1 OFF , 2 ON
        $ftx_lasertemp[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.2." . $slot_ftx[$i] . ".0"));
        // laser temp, C
        $ftx_laserbiascurent[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.3." . $slot_ftx[$i] . ".0"));
        // laer bias current, mA
        $ftx_outputpower[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.4." . $slot_ftx[$i] . ".0"));
        // laser output power, dBm
        $ftx_thermoeleccoolercurrent[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.5." . $slot_ftx[$i] . ".0"));
        // thermal electroc cooler current, mA
        $ftx_lasertype[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.6." . $slot_ftx[$i] . ".0"));
        // laser type
        $ftx_wavelength[] = deco_elink(snmpget_smallp($ip, ".1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.7." . $slot_ftx[$i] . ".0"));
        // laser wavelength, nm
    }
    // DB1	chech if the table "daemonsnmpelinksummary" in the database "vanguardhe"; this table is for MAC address and maybe other use
    $query_exist_sum = "SELECT relname FROM pg_class \n\tWHERE relname = 'daemonsnmpelinksummary';";
    $result_exist_sum = pg_query($query_exist_sum) or die('Query failed: ' . pg_last_error());
    $exist_sum = '';
    while ($row_exist_sum = pg_fetch_object($result_exist_sum)) {
        $exist_sum = $row_exist_sum->relname;
    }
    // if not existed, create it
    if ($exist_sum != "daemonsnmpelinksummary") {
        # code...
        $query_construct_sum = "CREATE TABLE PUBLIC.daemonsnmpelinksummary(\n\t\t\tmac           TEXT,\n\t\t\tdecription\tText,\n\t\t\tcomments            TEXT );";
        $result_construct_sum = pg_query($query_construct_sum) or die('Query failed: ' . pg_last_error());
        pg_free_result($result_construct_sum);
    }
    // DB2:  chech if the table "daemonsnmpelinkems" in the database "vanguardhe"; this table is for EMS
    $query_exist_ems = "SELECT relname FROM pg_class \n\tWHERE relname = 'daemonsnmpelinkems';";
    $result_exist_ems = pg_query($query_exist_ems) or die('Query failed: ' . pg_last_error());
    $exist_ems = '';
    while ($row_exist_ems = pg_fetch_object($result_exist_ems)) {
        $exist_ems = $row_exist_ems->relname;
    }
    // if not existed, create it
    if ($exist_ems != "daemonsnmpelinkems") {
        # code...
        $query_construct_ems = "CREATE TABLE PUBLIC.daemonsnmpelinkems(\n\t\t\tdeviceid int,\n\t\t\ttime           TEXT    ,\n\t\t\tslot\tTEXT,  \n\t\t\tdescription            TEXT  ,\n\t\t\toids       TEXT,\n\t\t\tuptime         TEXT,\n\t\t\tcontact         TEXT,\n\t\t\tname        TEXT,\n\t\t\tlocation         TEXT,\n\t\t\tservice        TEXT,\n\t\t\tip         TEXT,\n\t\t\talarm         TEXT,\n\t\t\tmodel        TEXT,\n\t\t\tsn       TEXT,\n\t\t\ttemp         TEXT,\n\t\t\tvendor        TEXT,\n\t\t\talarmdetection         TEXT,\n\t\t\tipaddress        TEXT,\n\t\t\tcheckcode         TEXT,\n\t\t\ttamperstatus         TEXT,\n\t\t\tinternaltemp        TEXT,\n\t\t\tcraftstatus       TEXT\n\t\t\t);";
        $result_construct_ems = pg_query($query_construct_ems) or die('Query failed: ' . pg_last_error());
        pg_free_result($result_construct_ems);
    }
    // insert data into the table
    $query_insert_ems = "INSERT INTO PUBLIC.daemonsnmpelinkems VALUES ({$deviceid}, {$timestamp}, {$pos_ems}, {$sysDescr}, {$sysObjectID}, {$sysUpTime}, {$sysContact}, {$sysName}, {$sysLocation}, {$sysService}, {$ipadd}, {$alarm}, {$ems}, {$ems_sn}, {$ems_temp}, {$nms['0']}, {$nms['1']}, {$nms['2']}, {$nms['3']}, {$nms['4']}, {$nms['5']}, {$nms['6']});";
    $result_insert_ems = pg_query($query_insert_ems) or die('Query failed: ' . pg_last_error());
    // DB3:  chech if the table "daemonsnmpelinkps" in the database "vanguardhe"; this table is for PS
    $query_exist_ps = "SELECT relname FROM pg_class \n\tWHERE relname = 'daemonsnmpelinkps';";
    $result_exist_ps = pg_query($query_exist_ps) or die('Query failed: ' . pg_last_error());
    $exist_ps = '';
    while ($row_exist_ps = pg_fetch_object($result_exist_ps)) {
        $exist_ps = $row_exist_ps->relname;
    }
    // if not existed, create it
    if ($exist_ps != "daemonsnmpelinkps") {
        # code...
        $query_construct_ps = "CREATE TABLE PUBLIC.daemonsnmpelinkps(\n\t\t\tdeviceid \tINT,\n\t\t\ttime           TEXT    ,\n\t\t\tslot\tTEXT,  \n\t\t\tmodel        TEXT,\n\t\t\tsn       TEXT,\n\t\t\ttemp         TEXT,\n\t\t\tinputv        TEXT,\n\t\t\toutputv         TEXT,\n\t\t\toutputma        TEXT,\n\t\t\toutputw         TEXT\n\t\t\t);";
        $result_construct_ps = pg_query($query_construct_ps) or die('Query failed: ' . pg_last_error());
        pg_free_result($result_construct_ps);
    }
    // insert data into the table
    $query_insert_ps = "INSERT INTO PUBLIC.daemonsnmpelinkps VALUES ({$deviceid}, {$timestamp}, {$pos_ps}, {$psu}, {$psu_sn}, {$psu_temp}, {$ps['0']}, {$ps['1']}, {$ps['2']}, {$ps['3']});";
    $result_insert_ps = pg_query($query_insert_ps) or die('Query failed: ' . pg_last_error());
    // DB4:  chech if the table "daemonsnmpelinkfan" in the database "vanguardhe"; this table is for fan
    $query_exist_fan = "SELECT relname FROM pg_class \n\tWHERE relname = 'daemonsnmpelinkfan';";
    $result_exist_fan = pg_query($query_exist_fan) or die('Query failed: ' . pg_last_error());
    $exist_fan = '';
    while ($row_exist_fan = pg_fetch_object($result_exist_fan)) {
        $exist_fan = $row_exist_fan->relname;
    }
    // if not existed, create it
    if ($exist_fan != "daemonsnmpelinkfan") {
        # code...
        $query_construct_fan = "CREATE TABLE PUBLIC.daemonsnmpelinkfan(\n\t\t\tdeivceid \tINT,\n\t\t\ttime           TEXT   , \n\t\t\tfan1        TEXT,\n\t\t\tfan2       TEXT,\n\t\t\tfan3         TEXT,\n\t\t\tfan4        TEXT,\n\t\t\tfan5         TEXT,\n\t\t\tfan6        TEXT,\n\t\t\tfan7         TEXT,\n\t\t\tfan8\t\tTEXT\n\t\t\t);";
        $result_construct_fan = pg_query($query_construct_fan) or die('Query failed: ' . pg_last_error());
        pg_free_result($result_construct_fan);
    }
    // insert data into the table
    $query_insert_fan = "INSERT INTO PUBLIC.daemonsnmpelinkfan VALUES ({$deviceid}, {$timestamp}, {$fan['0']}, {$fan['1']}, {$fan['2']}, {$fan['3']}, {$fan['4']}, {$fan['5']}, {$fan['6']}, {$fan['7']});";
    $result_insert_fan = pg_query($query_insert_fan) or die('Query failed: ' . pg_last_error());
    // DB5:  chech if the table "daemonsnmpelinkrrx" in the database "vanguardhe"; this table is for RRX
    $query_exist_rrx = "SELECT relname FROM pg_class \n\tWHERE relname = 'daemonsnmpelinkrrx';";
    $result_exist_rrx = pg_query($query_exist_rrx) or die('Query failed: ' . pg_last_error());
    $exist_rrx = '';
    while ($row_exist_rrx = pg_fetch_object($result_exist_rrx)) {
        $exist_rrx = $row_exist_rrx->relname;
    }
    // if not existed, create it
    if ($exist_rrx != "daemonsnmpelinkrrx") {
        # code...
        $query_construct_rrx = "CREATE TABLE PUBLIC.daemonsnmpelinkrrx(\n\t\t\tdeviceid \tINT,\n\t\t\ttime           TEXT    , \n\t\t\tslot\tTEXT,  \n\t\t\tmodel        TEXT,\n\t\t\tsn       TEXT,\n\t\t\ttemp         TEXT,\n\t\t\tinput1         TEXT,\n\t\t\tinput2        TEXT,\n\t\t\tinput3        TEXT,\n\t\t\tinput4\t\tTEXT,\n\t\t\tstatus1         TEXT,\n\t\t\tstatus2        TEXT,\n\t\t\tstatus3        TEXT,\n\t\t\tstatus4\t\tTEXT\n\t\t\t);";
        $result_construct_rrx = pg_query($query_construct_rrx) or die('Query failed: ' . pg_last_error());
        pg_free_result($result_construct_rrx);
    }
    // insert data
    for ($i = 0; $i < sizeof($slot_rrx); $i++) {
        // insert data into the table
        $query_insert_rrx = "INSERT INTO PUBLIC.daemonsnmpelinkrrx VALUES ({$deviceid}, {$timestamp}, {$pos_rrx[$i]}, {$rrx_name[$i]}, {$rrx_sn[$i]}, {$rrx_temp[$i]}, {$rrx_input1[$i]}, {$rrx_input2[$i]}, {$rrx_input3[$i]}, {$rrx_input4[$i]}, {$rrx_status1[$i]}, {$rrx_status2[$i]}, {$rrx_status3[$i]}, {$rrx_status4[$i]});";
        $result_insert_rrx = pg_query($query_insert_rrx) or die('Query failed: ' . pg_last_error());
    }
    // DB6:  chech if the table "daemonsnmpelinkftx" in the database "vanguardhe"; this table is for RRX
    $query_exist_ftx = "SELECT relname FROM pg_class \n\tWHERE relname = 'daemonsnmpelinkftx';";
    $result_exist_ftx = pg_query($query_exist_ftx) or die('Query failed: ' . pg_last_error());
    $exist_ftx = '';
    while ($row_exist_ftx = pg_fetch_object($result_exist_ftx)) {
        $exist_ftx = $row_exist_ftx->relname;
    }
    // if not existed, create it
    if ($exist_ftx != "daemonsnmpelinkftx") {
        # code...
        $query_construct_ftx = "CREATE TABLE PUBLIC.daemonsnmpelinkftx(\n\t\t\tdeviceid \tINT,\n\t\t\ttime           TEXT    , \n\t\t\tslot\tTEXT,  \n\t\t\tmodel        TEXT,\n\t\t\tsn       TEXT,\n\t\t\ttemp         TEXT,\n\t\t\trfinputpower         TEXT,\n\t\t\tagcmode        TEXT,\n\t\t\tlasertemp        TEXT,\n\t\t\tlaserbiascurrent\t\tTEXT,\n\t\t\toutputpower         TEXT,\n\t\t\tthccurrent        TEXT,\n\t\t\tlasertype        TEXT,\n\t\t\twavelength\t\tTEXT\n\t\t\t);";
        $result_construct_ftx = pg_query($query_construct_ftx) or die('Query failed: ' . pg_last_error());
        pg_free_result($result_construct_ftx);
    }
    // insert data
    for ($i = 0; $i < sizeof($slot_ftx); $i++) {
        // insert data into the table
        $query_insert_ftx = "INSERT INTO PUBLIC.daemonsnmpelinkftx VALUES ({$deviceid}, {$timestamp}, {$pos_ftx[$i]}, {$ftx_name[$i]}, {$ftx_sn[$i]}, {$ftx_temp[$i]}, {$ftx_rfinputpower[$i]}, {$ftx_agcmode[$i]}, {$ftx_lasertemp[$i]}, {$ftx_laserbiascurent[$i]}, {$ftx_outputpower[$i]}, {$ftx_thermoeleccoolercurrent[$i]}, {$ftx_lasertype[$i]}, {$ftx_wavelength[$i]});";
        $result_insert_ftx = pg_query($query_insert_ftx) or die('Query failed: ' . pg_last_error());
    }
    // pg_free_result($result_exist);
    // pg_free_result($result_insert);
    // pg_free_result($result_construct);
    pg_free_result($result_exist_sum);
    pg_free_result($result_exist_ems);
    pg_free_result($result_insert_ems);
    pg_free_result($result_exist_ps);
    pg_free_result($result_insert_ps);
    pg_free_result($result_exist_fan);
    pg_free_result($result_insert_fan);
    pg_free_result($result_exist_rrx);
    pg_free_result($result_insert_rrx);
    pg_free_result($result_exist_ftx);
    pg_free_result($result_insert_ftx);
    pg_close($dbconn);
}