Exemplo n.º 1
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     //
     $option_interval = $this->option('interval');
     $option_status = $this->option('status');
     if ($option_status == 'A') {
         // poll all
         $pollings = \App\Polling::where('table_name', 'ports')->where('interval', $option_interval)->get();
     } else {
         // poll only status = 'Y'
         $pollings = \App\Polling::where('table_name', 'ports')->where('status', 'Y')->where('interval', $option_interval)->get();
     }
     foreach ($pollings as $polling) {
         $port = \App\Port::findOrFail($polling->table_id);
         if ($port->poll_enabled == 'Y' || $option_status == 'A') {
             $poll_class = '\\App\\Lnms\\' . $port->node->poll_class . '\\' . $polling->poll_class;
             $poll_object = new $poll_class($port->node);
             $poll_method = $polling->poll_method;
             $poller_result = $poll_object->{$poll_method}($port->ifIndex);
             for ($i = 0; $i < count($poller_result); $i++) {
                 if ($poller_result[$i]['table'] == 'pds') {
                     unset($poller_result[$i]['key']);
                     $poller_result[$i]['key'] = ['polling_id' => $polling->id, 'timestamp' => \Carbon\Carbon::now()];
                 }
                 switch ($poller_result[$i]['action']) {
                     case 'insert':
                         // insert new
                         if (isset($poller_result[$i]['key'])) {
                             \DB::table($poller_result[$i]['table'])->insert(array_merge($poller_result[$i]['key'], $poller_result[$i]['data']));
                         } else {
                             \DB::table($poller_result[$i]['table'])->insert($poller_result[$i]['data']);
                         }
                         break;
                     case 'sync':
                     case 'update':
                         // query existing data by key
                         $poll_db = \DB::table($poller_result[$i]['table']);
                         foreach ($poller_result[$i]['key'] as $poll_key => $poll_value) {
                             $poll_db = $poll_db->where($poll_key, $poll_value);
                         }
                         if ($poll_db->count() > 0) {
                             // update
                             \DB::table($poller_result[$i]['table'])->where('id', $poll_db->first()->id)->update($poller_result[$i]['data']);
                         } else {
                             if ($poller_result[$i]['action'] == 'sync') {
                                 // just insert for 'sync'
                                 if (isset($poller_result[$i]['key'])) {
                                     \DB::table($poller_result[$i]['table'])->insert(array_merge($poller_result[$i]['key'], $poller_result[$i]['data']));
                                 } else {
                                     \DB::table($poller_result[$i]['table'])->insert($poller_result[$i]['data']);
                                 }
                             }
                         }
                         // TODO : detect and delete removed Port from DB
                         break;
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
 /**
  *
  */
 public function octets_data($id)
 {
     if (\Request::has('date')) {
         $q_date = \Request::get('date');
     } else {
         $q_date = strftime("%Y-%m-%d");
     }
     $port = \App\Port::findOrFail($id);
     $octets = \App\Octet::where('port_id', $id)->where('timestamp', '>', $q_date)->orderBy('timestamp')->limit(288)->get();
     //
     $prev_timestamp = 0;
     $prev_input = 0;
     $prev_output = 0;
     $avg_input = [];
     $avg_output = [];
     foreach ($octets as $data) {
         $diff_timestamp = strtotime($data->timestamp) - $prev_timestamp;
         $diff_input = $data->input - $prev_input;
         $diff_output = $data->output - $prev_output;
         if ($diff_input < 0) {
             $diff_input = $diff_input + 4294967296;
         }
         if ($diff_output < 0) {
             $diff_output = $diff_output + 4294967296;
         }
         if ($prev_input != 0 && $diff_timestamp > 0 && $diff_input >= 0 && $diff_output >= 0) {
             // convert byte to bit
             $in_avg5 = (int) (8 * ($diff_input / $diff_timestamp));
             $out_avg5 = (int) (8 * ($diff_output / $diff_timestamp));
             $avg_input[] = array(strtotime($data->timestamp) * 1000, $in_avg5);
             $avg_output[] = array(strtotime($data->timestamp) * 1000, $out_avg5);
         }
         // next
         $prev_timestamp = strtotime($data->timestamp);
         $prev_input = $data->input;
         $prev_output = $data->output;
     }
     $response[] = array('data' => $avg_input, 'label' => 'Inbound', 'color' => 'lightgreen', 'lines' => array('show' => 'true', 'fill' => 'true', 'fillColor' => 'lightgreen'));
     $response[] = array('data' => $avg_output, 'label' => 'Outbound', 'color' => 'darkblue');
     // Response::json($response, $statusCode)->setCallback(Input::get('callback'));i
     return response()->json($response);
 }