First Upload
This commit is contained in:
104
app/CustomClasses/ConfigSearch.php
Normal file
104
app/CustomClasses/ConfigSearch.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace App\CustomClasses;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Config;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ConfigSearch
|
||||
{
|
||||
protected $categoryId;
|
||||
|
||||
protected $categoryName;
|
||||
|
||||
protected $string;
|
||||
|
||||
protected $linecount;
|
||||
|
||||
protected $latestOnly;
|
||||
|
||||
public function __construct($categoryId, $string, $linecount, $latestOnly)
|
||||
{
|
||||
$this->categoryId = $categoryId;
|
||||
$this->string = $string;
|
||||
$this->linecount = $linecount;
|
||||
$this->latestOnly = isset($latestOnly) ? $latestOnly : false;
|
||||
}
|
||||
|
||||
public function search()
|
||||
{
|
||||
$results = null;
|
||||
if ($this->latestOnly === false) {
|
||||
$results = $this->globalSearch();
|
||||
} elseif ($this->latestOnly === true) {
|
||||
$results = $this->latestSearch();
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
private function latestSearch()
|
||||
{
|
||||
$this->categoryName = Category::where('id', $this->categoryId)->first()->categoryName;
|
||||
$deviceIdsForGivenCat = Config::where('device_category', $this->categoryName)->distinct()->pluck('device_id');
|
||||
|
||||
$latestConfigsArray = [];
|
||||
foreach ($deviceIdsForGivenCat as $device) {
|
||||
$created_at = DB::table('configs')->where('device_id', $device)->max('created_at');
|
||||
$latestConfigsArray[] = Config::where('created_at', $created_at)->get();
|
||||
}
|
||||
$latestConfigsArray = Arr::flatten($latestConfigsArray);
|
||||
|
||||
$allResultsArr = [];
|
||||
$matchCount = 0;
|
||||
foreach ($latestConfigsArray as $latestConfigItem) {
|
||||
$grepSearchCommand = 'grep '.escapeshellarg($this->string).' '.$latestConfigItem->config_location.' -C'.$this->linecount;
|
||||
exec($grepSearchCommand, $searchresults);
|
||||
//$searchresults0 & 1 are intended to mimic the output from the $searchresults arary in the globalSearch func
|
||||
if (! empty($searchresults)) {
|
||||
array_unshift($searchresults, '', '-::'.$latestConfigItem->config_location.'::-'); // move these to the top of the array
|
||||
$allResultsArr[] = $searchresults;
|
||||
$matchCount++;
|
||||
}
|
||||
unset($searchresults);
|
||||
}
|
||||
|
||||
$allResultsArr = Arr::flatten($allResultsArr);
|
||||
|
||||
$results = [];
|
||||
$start_time = Carbon::now();
|
||||
$results['lineCount'] = count($allResultsArr); // take away duration & linecount keys
|
||||
$results['matchCount'] = $matchCount;
|
||||
|
||||
$results['fileCount'] = $matchCount;
|
||||
$end_time = Carbon::now();
|
||||
$results['duration'] = $start_time->diffInMilliseconds($end_time).'ms';
|
||||
$results['search_results'] = $allResultsArr;
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function globalSearch()
|
||||
{
|
||||
$awkSearchCommand = 'find '.config_data_path().$this->categoryName.' -type f -name "*.txt" | xargs grep -n -C'.$this->linecount.' '.escapeshellarg($this->string).' | awk -f '.rconfig_app_path().'search.awk';
|
||||
|
||||
$totalMatchesCommand = 'grep -ro '.escapeshellarg($this->string).' '.config_data_path().'/ | wc -l ';
|
||||
$results = [];
|
||||
$start_time = Carbon::now();
|
||||
exec($awkSearchCommand, $searchresults);
|
||||
exec($totalMatchesCommand, $matchCount);
|
||||
$results['lineCount'] = count($searchresults); // take away duration & linecount keys
|
||||
$results['matchCount'] = (int) $matchCount[0]; // take away duration & linecount keys
|
||||
$results['fileCount'] = count(array_filter($searchresults, function ($v) {
|
||||
return substr($v, 0, 3) === '-::';
|
||||
}));
|
||||
$end_time = Carbon::now();
|
||||
$results['duration'] = $start_time->diffInMilliseconds($end_time).'ms';
|
||||
$results['search_results'] = $searchresults;
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
40
app/CustomClasses/CreateTaskReport.php
Normal file
40
app/CustomClasses/CreateTaskReport.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\CustomClasses;
|
||||
|
||||
use App\Models\Taskdownloadreport;
|
||||
use Illuminate\Support\Facades\File;
|
||||
|
||||
class CreateTaskReport
|
||||
{
|
||||
public $report_data;
|
||||
|
||||
public function __construct($report_data)
|
||||
{
|
||||
$this->report_data = $report_data;
|
||||
}
|
||||
|
||||
public function saveReport()
|
||||
{
|
||||
$this->report_data->duration = $this->report_data->end_time->diffInSeconds($this->report_data->start_time);
|
||||
|
||||
File::put(
|
||||
$this->report_data->report_path,
|
||||
view('report_templates.task_report')
|
||||
->with(['report_data' => $this->report_data])
|
||||
->render()
|
||||
);
|
||||
|
||||
$model = new Taskdownloadreport;
|
||||
$model->report_id = $this->report_data->report_id;
|
||||
$model->task_type = $this->report_data->task_type;
|
||||
$model->task_id = $this->report_data->task->id;
|
||||
$model->task_name = $this->report_data->task->task_name;
|
||||
$model->task_desc = $this->report_data->task->task_desc;
|
||||
$model->file_name = $this->report_data->file_name;
|
||||
$model->start_time = $this->report_data->start_time->toDateTimeString();
|
||||
$model->end_time = $this->report_data->end_time->toDateTimeString();
|
||||
$model->duration = $this->report_data->duration;
|
||||
$model->save();
|
||||
}
|
||||
}
|
1404
app/CustomClasses/CronSchedule.php
Normal file
1404
app/CustomClasses/CronSchedule.php
Normal file
File diff suppressed because it is too large
Load Diff
32
app/CustomClasses/Debug.php
Normal file
32
app/CustomClasses/Debug.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\CustomClasses;
|
||||
|
||||
use App\Models\Setting;
|
||||
|
||||
class Debug
|
||||
{
|
||||
private $_debugState;
|
||||
|
||||
public function __construct($debugState = false)
|
||||
{
|
||||
$this->_debugState = $debugState;
|
||||
}
|
||||
|
||||
public function debugDump($message)
|
||||
{
|
||||
if (\Request::segment(1) == 'web') {
|
||||
return;
|
||||
} // no debug on web invokation
|
||||
|
||||
$debugging = Setting::find(1)->value('deviceDebugging');
|
||||
if ($this->debugcheck() == 1 || $this->_debugState == true) {
|
||||
dump($message);
|
||||
}
|
||||
}
|
||||
|
||||
public function debugcheck()
|
||||
{
|
||||
return Setting::find(1)->value('deviceDebugging');
|
||||
}
|
||||
}
|
96
app/CustomClasses/DeviceDownloadClass.php
Normal file
96
app/CustomClasses/DeviceDownloadClass.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace App\CustomClasses;
|
||||
|
||||
use App\Http\Controllers\Connections\MainConnectionManager;
|
||||
use App\Models\User;
|
||||
use App\Notifications\DBDeviceConnectionFailureNotification;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
|
||||
class DeviceDownloadClass extends Command
|
||||
{
|
||||
protected $output;
|
||||
|
||||
protected $eventtype;
|
||||
|
||||
protected $devicerecords;
|
||||
|
||||
protected $debug;
|
||||
|
||||
protected $report_id;
|
||||
|
||||
protected $parent_function;
|
||||
|
||||
protected $parent_class;
|
||||
|
||||
public function __construct($devicerecords, $eventtype, $debug, $report_id = null)
|
||||
{
|
||||
$this->output = [];
|
||||
$this->eventtype = $eventtype;
|
||||
$this->devicerecords = $devicerecords;
|
||||
$this->debug = $debug;
|
||||
$this->report_id = $report_id;
|
||||
$this->parent_class = get_parent_class();
|
||||
$this->parent_function = debug_backtrace()[1]['function'];
|
||||
}
|
||||
|
||||
public function downloadDevices()
|
||||
{
|
||||
// start the download
|
||||
foreach ($this->devicerecords as $devicerecord) {
|
||||
$devicerecord['start_time'] = Carbon::now();
|
||||
|
||||
$logmsg = 'Start device download for ' . ($devicerecord['device_name'] . ' ID:' . $devicerecord['id']);
|
||||
activityLogIt($this->parent_class, $this->parent_function, 'info', $logmsg, 'connection', $devicerecord['device_name'], $devicerecord['id'], $this->eventtype, $devicerecord['id']);
|
||||
$this->output['info'][] = $logmsg;
|
||||
|
||||
$devicerecord = (new DeviceRecordPrepare($devicerecord))->DeviceRecordToArray();
|
||||
$connectionObj = new MainConnectionManager($devicerecord, $this->debug);
|
||||
$configsArray = $connectionObj->setupConnectAndReturnOutput();
|
||||
|
||||
// throw and error if configsArray is false
|
||||
if ($configsArray === false || isset($configsArray['failure'])) {
|
||||
$devicerecord['end_time'] = Carbon::now();
|
||||
$logmsg = 'No config data returned for ' . ($devicerecord['device_name'] . ' - ID:' . $devicerecord['id'] . '. Check your logs for more information');
|
||||
Notification::send(User::all(), new DBDeviceConnectionFailureNotification($logmsg, $devicerecord['id']));
|
||||
|
||||
$configSaveResult = (new SaveConfigsToDiskAndDb('device_download', 'Failed config download', 0, $devicerecord, $this->report_id))->saveConfigs();
|
||||
|
||||
$this->output['error'][] = $logmsg;
|
||||
activityLogIt($this->parent_class, $this->parent_function, 'error', $logmsg, 'connection', $devicerecord['device_name'], $devicerecord['id'], $this->eventtype, $devicerecord['id']);
|
||||
|
||||
(new SetDeviceStatus($devicerecord['id'], 0))->setDeviceStatus();
|
||||
|
||||
continue; // continue looping around if more devices
|
||||
}
|
||||
(new SetDeviceStatus($devicerecord['id'], 1))->setDeviceStatus();
|
||||
$logmsg = 'End device download for ' . ($devicerecord['device_name'] . ' ID:' . $devicerecord['id']);
|
||||
activityLogIt($this->parent_class, $this->parent_function, 'info', $logmsg, 'connection', $devicerecord['device_name'], $devicerecord['id'], $this->eventtype, $devicerecord['id']);
|
||||
|
||||
if (isset($configsArray['failure'])) {
|
||||
$this->output['error'][] = $configsArray['failure'];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($configsArray as $commandName => $configArray) {
|
||||
$devicerecord['end_time'] = Carbon::now();
|
||||
$configSaveResult = (new SaveConfigsToDiskAndDb('device_download', $commandName, $configArray, $devicerecord, $this->report_id))->saveConfigs();
|
||||
$configresultText = $configSaveResult['success'] === true ? ' was successful' : ' failed';
|
||||
$logmsg = 'Config downloaded for ' . $devicerecord['device_name'] . ' with command: "' . $configSaveResult['commandName'] . '"' . $configresultText;
|
||||
$this->output['info'][] = $logmsg;
|
||||
activityLogIt($this->parent_class, $this->parent_function, 'info', $logmsg, 'connection', $devicerecord['device_name'], $devicerecord['id'], 'device');
|
||||
}
|
||||
|
||||
// dd($configCheckResult);
|
||||
}
|
||||
if (isset($devicerecord)) {
|
||||
Redis::set('download-now-' . $devicerecord['id'], 'false');
|
||||
}
|
||||
|
||||
return $this->output;
|
||||
}
|
||||
}
|
38
app/CustomClasses/DeviceRecordPrepare.php
Normal file
38
app/CustomClasses/DeviceRecordPrepare.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\CustomClasses;
|
||||
|
||||
use App\Models\Category;
|
||||
|
||||
class DeviceRecordPrepare
|
||||
{
|
||||
protected $devicerecord;
|
||||
|
||||
public function __construct(object $devicerecord)
|
||||
{
|
||||
$this->devicerecord = $devicerecord;
|
||||
}
|
||||
|
||||
public function DeviceRecordToArray()
|
||||
{
|
||||
$commands = $this->_getCommands($this->devicerecord->device_category_id);
|
||||
|
||||
if ($commands->isEmpty()) {
|
||||
// this is useful for some tests or when a devices category has no commands
|
||||
return $commands[0] = [];
|
||||
}
|
||||
|
||||
$devicerecordArr = $this->devicerecord->toArray();
|
||||
|
||||
foreach ($commands[0]->command as $command) {
|
||||
$devicerecordArr['commands'][$command->id] = $command->command;
|
||||
}
|
||||
|
||||
return $devicerecordArr;
|
||||
}
|
||||
|
||||
private function _getCommands($device_category_id)
|
||||
{
|
||||
return Category::with('command')->where('id', $device_category_id)->get();
|
||||
}
|
||||
}
|
19
app/CustomClasses/FilterArgsForCommnds.php
Normal file
19
app/CustomClasses/FilterArgsForCommnds.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\CustomClasses;
|
||||
|
||||
class FilterArgsForCommnds
|
||||
{
|
||||
private $args;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function filterArgs($args)
|
||||
{
|
||||
$this->args = $args;
|
||||
|
||||
return array_filter($this->args, 'ctype_digit');
|
||||
}
|
||||
}
|
20
app/CustomClasses/GetAndCheckCategoryIds.php
Normal file
20
app/CustomClasses/GetAndCheckCategoryIds.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\CustomClasses;
|
||||
|
||||
use App\Models\Category;
|
||||
|
||||
class GetAndCheckCategoryIds
|
||||
{
|
||||
protected $ids;
|
||||
|
||||
public function __construct(array $ids)
|
||||
{
|
||||
$this->ids = $ids;
|
||||
}
|
||||
|
||||
public function GetCategoryRecords()
|
||||
{
|
||||
return Category::with('device')->whereIn('id', $this->ids)->get();
|
||||
}
|
||||
}
|
20
app/CustomClasses/GetAndCheckDeviceIds.php
Normal file
20
app/CustomClasses/GetAndCheckDeviceIds.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\CustomClasses;
|
||||
|
||||
use App\Models\Device;
|
||||
|
||||
class GetAndCheckDeviceIds
|
||||
{
|
||||
protected $ids;
|
||||
|
||||
public function __construct(array $ids)
|
||||
{
|
||||
$this->ids = $ids;
|
||||
}
|
||||
|
||||
public function GetDeviceRecords()
|
||||
{
|
||||
return Device::with('category')->whereIn('id', $this->ids)->get();
|
||||
}
|
||||
}
|
20
app/CustomClasses/GetAndCheckTagIds.php
Normal file
20
app/CustomClasses/GetAndCheckTagIds.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\CustomClasses;
|
||||
|
||||
use App\Models\Tag;
|
||||
|
||||
class GetAndCheckTagIds
|
||||
{
|
||||
protected $ids;
|
||||
|
||||
public function __construct(array $ids)
|
||||
{
|
||||
$this->ids = $ids;
|
||||
}
|
||||
|
||||
public function GetTagRecords()
|
||||
{
|
||||
return Tag::with('device')->whereIn('id', $this->ids)->get();
|
||||
}
|
||||
}
|
20
app/CustomClasses/GetAndCheckTaskIds.php
Normal file
20
app/CustomClasses/GetAndCheckTaskIds.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\CustomClasses;
|
||||
|
||||
use App\Models\Task;
|
||||
|
||||
class GetAndCheckTaskIds
|
||||
{
|
||||
protected $ids;
|
||||
|
||||
public function __construct(array $ids)
|
||||
{
|
||||
$this->ids = $ids;
|
||||
}
|
||||
|
||||
public function GetTaskRecords()
|
||||
{
|
||||
return Task::with('device', 'tag', 'category')->whereIn('id', $this->ids)->get();
|
||||
}
|
||||
}
|
69
app/CustomClasses/PurgeOperations.php
Normal file
69
app/CustomClasses/PurgeOperations.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\CustomClasses;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Notifications\DBPurgeOperationNotification;
|
||||
use App\Notifications\MailPurgeOperationNotification;
|
||||
use File;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Validator;
|
||||
|
||||
class PurgeOperations
|
||||
{
|
||||
public function purge(Request $request)
|
||||
{
|
||||
Validator::make($request->all(), [
|
||||
'days' => 'required|integer|gt:0',
|
||||
])->validate();
|
||||
|
||||
switch ($request->purgetype) {
|
||||
case 'backup':
|
||||
$files = File::allFiles(backup_path());
|
||||
break;
|
||||
case 'settings':
|
||||
$files = File::allFiles(storage_path().'/logs');
|
||||
break;
|
||||
}
|
||||
|
||||
if (! empty($files)) {
|
||||
$purgeSeconds = 86400 * $request->days;
|
||||
$limit = time() - $purgeSeconds;
|
||||
$purgelist = [];
|
||||
foreach ($files as $file) {
|
||||
if ($file->getMtime() < $limit) {
|
||||
$purgelist[] = $file;
|
||||
}
|
||||
}
|
||||
|
||||
File::delete($purgelist);
|
||||
|
||||
$msg = $request->purgetype.' files older than '.$request->days.' days are now purged.';
|
||||
$output = [
|
||||
'success' => true,
|
||||
'data' => '',
|
||||
'msg' => $msg,
|
||||
];
|
||||
|
||||
// send notification
|
||||
try {
|
||||
Notification::send(User::allUsersAndRecipients(), new MailPurgeOperationNotification($msg, $this->username));
|
||||
Notification::send(User::all(), new DBPurgeOperationNotification($msg, $this->username));
|
||||
|
||||
$responseArray = ['success' => 200, 'msg' => $msg];
|
||||
activityLogIt(__CLASS__, __FUNCTION__, 'warn', $responseArray['msg'], 'purge');
|
||||
} catch (\Exception $e) {
|
||||
activityLogIt(__CLASS__, __FUNCTION__, 'error', $e->getMessage(), 'purge');
|
||||
}
|
||||
} else {
|
||||
//LOGGING
|
||||
$output = [
|
||||
'success' => false,
|
||||
'msg' => 'No files found!',
|
||||
];
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
74
app/CustomClasses/SaveConfigsToDiskAndDb.php
Normal file
74
app/CustomClasses/SaveConfigsToDiskAndDb.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\CustomClasses;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Config;
|
||||
use App\Services\Config\FileOperations;
|
||||
|
||||
class SaveConfigsToDiskAndDb
|
||||
{
|
||||
public $configsArray;
|
||||
|
||||
public $type;
|
||||
|
||||
public $commandName;
|
||||
|
||||
public $devicerecord;
|
||||
|
||||
public $report_id;
|
||||
|
||||
public function __construct($type, $commandName, $configsArray, $devicerecord, $report_id = null)
|
||||
{
|
||||
$this->type = $type;
|
||||
$this->commandName = $commandName;
|
||||
$this->configsArray = $configsArray;
|
||||
$this->devicerecord = $devicerecord;
|
||||
$this->report_id = $report_id;
|
||||
}
|
||||
|
||||
public function saveConfigs()
|
||||
{
|
||||
$savedFileInfo = null;
|
||||
$device_category = Category::where('id', $this->devicerecord['device_category_id'])->pluck('categoryName')->first();
|
||||
$duration = $this->devicerecord['end_time']->diffInSeconds($this->devicerecord['start_time']);
|
||||
|
||||
if ($this->configsArray != 0) {
|
||||
$fileops =
|
||||
new FileOperations(
|
||||
$this->commandName,
|
||||
$device_category,
|
||||
$this->devicerecord['device_name'],
|
||||
$this->devicerecord['id'],
|
||||
config_data_path(),
|
||||
$this->type
|
||||
);
|
||||
$savedFileInfo = $fileops->saveFile($this->configsArray);
|
||||
}
|
||||
|
||||
$model = new Config;
|
||||
$model->device_id = $this->devicerecord['id'];
|
||||
$model->device_name = $this->devicerecord['device_name'];
|
||||
$model->device_category = $device_category;
|
||||
$model->command = $this->commandName;
|
||||
$model->type = $this->type;
|
||||
if ($this->configsArray === 0 || $this->configsArray === null) {
|
||||
$model->download_status = 0;
|
||||
} else {
|
||||
$model->config_location = $savedFileInfo['filepath'];
|
||||
$model->config_filename = $savedFileInfo['filename'];
|
||||
$model->config_filesize = $savedFileInfo['filesize'];
|
||||
$model->download_status = $savedFileInfo['download_status'];
|
||||
}
|
||||
$model->report_id = $this->report_id;
|
||||
$model->start_time = $this->devicerecord['start_time']->toDateTimeString();
|
||||
$model->end_time = $this->devicerecord['end_time']->toDateTimeString();
|
||||
$model->duration = $duration;
|
||||
$model->save();
|
||||
if ($model->save() && !empty($savedFileInfo)) {
|
||||
return ['success' => true, 'commandName' => $this->commandName];
|
||||
}
|
||||
|
||||
return ['success' => false, 'commandName' => $this->commandName];
|
||||
}
|
||||
}
|
27
app/CustomClasses/SetDeviceStatus.php
Normal file
27
app/CustomClasses/SetDeviceStatus.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\CustomClasses;
|
||||
|
||||
use App\Models\Device;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class SetDeviceStatus
|
||||
{
|
||||
protected $status;
|
||||
|
||||
protected $deviceid;
|
||||
|
||||
public function __construct($deviceid, $status)
|
||||
{
|
||||
$this->status = $status;
|
||||
$this->deviceid = $deviceid;
|
||||
}
|
||||
|
||||
public function setDeviceStatus()
|
||||
{
|
||||
$device = Device::findOrFail($this->deviceid);
|
||||
$device->last_seen = Carbon::now()->toDateTimeString();
|
||||
$device->status = $this->status;
|
||||
$device->save();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user