Logging.php

- apc cache and insert to db
- seeder
- users table add columns
This commit is contained in:
2019-11-21 11:17:01 +01:00
parent ffb9742efb
commit 8caa7c227b
3 changed files with 131 additions and 14 deletions

View File

@@ -10,12 +10,58 @@ class Logging extends Controller
private $_items = [ 'netstat_sent' => 'i', 'netstat_recv' => 'i', 'memory_free' => 'f', 'memory_total' => 'f', 'memory_used' => 'f', 'disk_free' => 'B', 'disk_percent' => 'f', 'disk_used' => 'B', 'disk_total' => 'B', 'processes' => 'i', 'temp' => 'i', 'username' =>'s', 'computer' => 's'];
private $_values = [];
private $_values = [];
private $_ucache = [];
private $_ccache = [];
public function __construct() {
$this->set_cache();
}
private function set_cache() {
$this->_ucache = \apcu_fetch('users',$ret);
if ($ret == false) {
$q = DB::table('users')->select('id', 'name')->get();
foreach ($q as $v) {
$this->_ucache[$v->name] = $v->id;
}
\apcu_add('users',$this->_ucache);
}
$this->_ccache = \apcu_fetch('computers',$ret);
if ($ret == false ) {
$q = DB::table('computers')->select('id', 'computer')->get();
foreach ($q as $v) {
$this->_ucache[$v->computer] = $v->id;
}
\apcu_add('computers',$this->_ccache);
}
}
private function get_computer_id($comp){
if ($this->_ccache[$comp]) {
return $this->_ccache[$comp];
}
$id = DB::table('computers')->insertGetId(['computer' => $comp]);
$this->_ccache[$comp] = $id;
\apcu_add('computers',$this->_ccache);
return $id;
}
private function get_user_id($user) {
if ($this->_ucache[$user]) {
return $this->_ucache[$user];
}
$id = DB::table('users')->insertGetId(['name' => $user]);
$this->_ucache[$user] = $id;
\apcu_add('users',$this->_ucache);
return $id;
}
function conv_bytes($v) {
$units = explode(' ', ' K M G T P E Z Y');
@@ -61,18 +107,32 @@ class Logging extends Controller
$this->_values[$k] = $val;
}
}
}
public function log(Request $request) {
$data = json_decode($request->getContent(), true);
$v = $data["values"];
$i = $data["items"];
public function log(Request $request) {
$data = json_decode($request->getContent(), true);
$v = $data["values"];
$i = $data["items"];
$this->parse($v);
$this->parse($v);
var_export($this->_values);
$ia = array_filter($this->_values, function($v) {
return $v != 's';
});
return "OK";
//DB::insert('insert into values (user,computer,recv,send) values (?, ?, ?, ?)', [$u, $c, $r, $s]);
}
$cid = $this->get_computer_id($this->_values["computer"]);
$uid = $this->get_user_id($this->_values["username"]);
$ia["computer_id"] = $cid;
DB::table('values')->insert($ia);
$uipa = [];
$uipa["computer_id"] = $cid;
$uipa["user_id"] = $uid;
$uipa["ip"] = $_SERVER['REMOTE_ADDR'];
DB::table('user_ip')->insert($uipa);
return "OK";
}
}

View File

@@ -18,6 +18,8 @@ class CreateUsersTable extends Migration
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('full_name');
$table->string('ou');
$table->rememberToken();
$table->timestamps();

View File

@@ -2,6 +2,32 @@
use Illuminate\Database\Seeder;
function csv_to_array($filename='', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($str = fgets($handle, 1000)) !== FALSE)
{
$str = iconv( "Windows-1250", "UTF-8", $str);
$row = str_getcsv ($str,$delimiter);
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
class DatabaseSeeder extends Seeder
{
/**
@@ -11,6 +37,35 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call('UsersTableSeeder');
}
}
class UsersTableSeeder extends Seeder {
public function run()
{
DB::table('users')->delete();
$aa = csv_to_array('C:\\Devel\\Users.csv',';');
$na = [];
foreach ($aa as $r) {
$s = $r["MENO"];
$a = explode(" ", $s);
$a[0] = str_slug($a[0]);
$a[1] = str_slug($a[1]);
$l = strtolower($a[0]);
$r["PASS"] = Hash::make(strtoupper($a[1][0]).$r["OC"].$a[0][0]);
$r["EMAIL"] = ucfirst($a[1]).".".ucfirst($a[0])."@minv.sk";
$r["LOGIN"] = $l.$r["OC"];
$na[] = $r;
}
foreach ($na as $r) {
DB::insert('insert into users (name,full_name,email,password,ou) values (?, ?, ?, ?, ?)', [$r["LOGIN"], $r["MENO"],$r["EMAIL"], $r["PASS"], $r["OU"]]);
}
}
}