#!/usr/bin/php
<?php

//$local = mysql_connect('localhost', 'xml', 'embasy');
//mysql_select_db('xml',$local);

$mysqli = new mysqli("localhost", "otdr", "]RM!iNyY2vJLE50]", "otdr");

// Check connection
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli->connect_error;
  exit();
}

$waktuz = date("Y-m-d H:i:s");

error_reporting(1);

$message = "";
$fd = fopen("php://stdin", "r");
while (!feof($fd))
  $message .= fread($fd, 1024);
$file = '/var/www/html/otdr/public/trap_receiver/snmptrapd.log';
$file_lain = '/var/www/html/otdr/public/trap_receiver/file_lain.log';
$file_debug = '/var/www/html/otdr/public/trap_receiver/debug_192.168.54.23.log';
$file_debug_nec = '/var/www/html/otdr/public/trap_receiver/debug_192.168.122.12.log';
$file_log = '/var/www/html/otdr/public/trap_receiver/debug_peak.log';
if (false !== strpos($message, '1.3.6.1.4.1.119.2.3.69.501.2.1.')) {
  file_put_contents($file, $message, FILE_APPEND);
} elseif (false !== strpos($message, '192.168.54.23')) {
  file_put_contents($file_debug, $message, FILE_APPEND);
} elseif (false !== strpos($message, '192.168.122.12')) {
  file_put_contents($file_debug_nec, $message, FILE_APPEND);
} else {
  file_put_contents($file_lain, $message, FILE_APPEND);
}

if (preg_match('/\[(.*?)\]/', $message, $matches)) { //NEC
  $otu_ip = $matches[1];
  preg_match_all('/\".*?\"/', $message, $matches_2);
  file_put_contents($file_log, __LINE__ . "\n" . print_r($matches_2, true), FILE_APPEND);
  $otu_sn = $matches_2[0][0];
  $otu_sn = str_replace("\"", "", $otu_sn);
  $otu_port = $matches_2[0][1];
  $otu_port = str_replace("\"", "", $otu_port);
  file_put_contents($file_log, __LINE__ . "\n" . "\n>>" . $otu_port . "<<\n", FILE_APPEND);
  if($otu_port == 'Missing peak(s)'){
    file_put_contents($file_log, __LINE__ . "\n", FILE_APPEND);
    $port_desc = $matches_2[0][10];
    $port_desc = str_replace("\"", "", $port_desc);
    $fault_loc_db = $matches_2[0][13];
    $fault_loc_db = str_replace("\"", "", $fault_loc_db);
    $alarm_distance = $matches_2[0][12];
    $alarm_distance = str_replace("\"", "", $alarm_distance);
    $probable_cause = $matches_2[0][11];
    $probable_cause = str_replace("\"", "", $probable_cause);
    $probable_cause = $probable_cause . " Missing Peak(s)";
    preg_match('/7\s(\d)/', $message, $matches_3);
    file_put_contents($file_log, __LINE__ . "\n" . print_r($matches_3, true), FILE_APPEND);
    $severity = $matches_3[1];
    file_put_contents($file_log, __LINE__ . "\n", FILE_APPEND);
    $pesan = "Alarm type: OPTICAL\nTimestamp: " . $waktuz . "\nSeverity: Major \nLink name: " . $port_desc . "\nProbable cause: " . $probable_cause . "\nOptical distance: " . $alarm_distance . "KM";
    $otu_port = $matches_2[0][2];
    $otu_port = str_replace("\"", "", $otu_port);
  }elseif($otu_port == 'No fault detected'){
      file_put_contents($file_log, __LINE__ . "\n", FILE_APPEND);
    $port_desc = $matches_2[0][10];
    $port_desc = str_replace("\"", "", $port_desc);
    $fault_loc_db = $matches_2[0][13];
    $fault_loc_db = str_replace("\"", "", $fault_loc_db);
    $alarm_distance = $matches_2[0][12];
    $alarm_distance = str_replace("\"", "", $alarm_distance);
    $probable_cause = $matches_2[0][11];
    $probable_cause = str_replace("\"", "", $probable_cause);
    $probable_cause = $probable_cause . " Missing Peak(s)";
    preg_match('/7\s(\d)/', $message, $matches_3);
    file_put_contents($file_log, __LINE__ . "\n" . print_r($matches_3, true), FILE_APPEND);
    $severity = $matches_3[1];
    file_put_contents($file_log, __LINE__ . "\n", FILE_APPEND);
    $pesan = "Alarm type: OPTICAL\nTimestamp: " . $waktuz . "\nSeverity: Clear \nLink name: " . $port_desc . "\nProbable cause: " . $probable_cause . "\nOptical distance: " . $alarm_distance . "KM";
    $otu_port = $matches_2[0][2];
    $otu_port = str_replace("\"", "", $otu_port);
  }else{
        file_put_contents($file_log, __LINE__ . "\n", FILE_APPEND);
        $port_desc = $matches_2[0][4];
        $port_desc = str_replace("\"", "", $port_desc);
        $fault_loc_db = $matches_2[0][5];
        $fault_loc_db = str_replace("\"", "", $fault_loc_db);
        $alarm_distance = $matches_2[0][6];
        $alarm_distance = str_replace("\"", "", $alarm_distance);
        $probable_cause = $matches_2[0][8];
        $probable_cause = str_replace("\"", "", $probable_cause);
        preg_match('/7\s(\d)/', $message, $matches_3);
        $severity = $matches_3[1];
        preg_match('/\"RTU.*\n.*\n.*\n.*\n.*\n.*\n.*\"/', $message, $matches_4);
        $pesan = $matches_4[0];
        $pesan = str_replace("\"", "", $pesan);
  }
}

//Download Alarm SOR
if ($severity != '1') {
  file_put_contents($file_log, __LINE__ . "\n", FILE_APPEND);
  $waktu = date("_Ymd-His");
  $port_number = substr($otu_port, -1);
  $trace_type = 'ALARM';
  $filename = '/home/syaiful/export/ALARM_' . $port_number . $waktu . '.sor';
  //$fp = fopen($filename, 'w+');
  $ch2 = curl_init();
  file_put_contents($file_log, __LINE__ . "\n", FILE_APPEND);
  curl_setopt($ch2, CURLOPT_URL, 'http://192.168.105.2/acquisitions/files');
  curl_setopt($ch2, CURLOPT_BINARYTRANSFER, 1);
  //curl_setopt($ch, CURLOPT_FILE, $fp);
  curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch2, CURLOPT_POST, 1);
  curl_setopt($ch2, CURLOPT_POSTFIELDS, '{"Port_Number":' . $port_number . ', "Trace_Type":"' . $trace_type . '"}');
  curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic YWRtaW46VDNsazBtYWtzZXNA', 'Content-disposition: attachment'));
  file_put_contents($file_log, __LINE__ . "\n", FILE_APPEND);
  $result = curl_exec($ch2);
  file_put_contents($filename, $result);
  file_put_contents($file_log, __LINE__ . "\n", FILE_APPEND);
  curl_close($ch2);
} else {
  file_put_contents($file_log, __LINE__ . "\n", FILE_APPEND);
  $filename = 'NONE';
}
file_put_contents($file_log, __LINE__ . "\n", FILE_APPEND);
$q_insert = "INSERT INTO trap_message (otu_ip, otu_sn, port, port_desc, severity, fault_loc_db, distance, probable_cause, message, time, file_name) VALUES ('$otu_ip', '$otu_sn', '$otu_port', '$port_desc', '$severity', '$fault_loc_db', '$alarm_distance', '$probable_cause', '$pesan', '$waktuz', '$filename')";
file_put_contents($file_log, __LINE__ . "\n" . $q_insert, FILE_APPEND);
if ($mysqli->query($q_insert) === TRUE) {
  file_put_contents($file_log, __LINE__ . "\n", FILE_APPEND);
  //echo "New record created successfully";
} else {
//  echo "Error: " . $sql . "<br>" . $mysqli->error;
  file_put_contents($file_log, __LINE__ . "\n" . $mysqli->error, FILE_APPEND);
}
file_put_contents($file_log, __LINE__ . "\n", FILE_APPEND);
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://192.168.201.10/catalyst_api/otdr/index.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "message=$pesan");
file_put_contents($file_log, __LINE__ . "\n", FILE_APPEND);
// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);
curl_close($ch);
file_put_contents($file_log, __LINE__ . "\n", FILE_APPEND);
