mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 15:24:35 +01:00
#43 new: bsd base support
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'package:toolbox/data/res/status.dart';
|
||||
|
||||
import 'time_seq.dart';
|
||||
|
||||
class Cpus extends TimeSeq<OneTimeCpuStatus> {
|
||||
@@ -95,3 +97,22 @@ List<OneTimeCpuStatus> parseCPU(String raw) {
|
||||
}
|
||||
return cpus;
|
||||
}
|
||||
|
||||
final _bsdCpuPercentReg = RegExp(r'(\d+\.\d+)%');
|
||||
|
||||
/// TODO: Change this implementation to parse cpu status on BSD system
|
||||
///
|
||||
/// [raw]:
|
||||
/// CPU usage: 14.70% user, 12.76% sys, 72.52% idle
|
||||
Cpus parseBsdCpu(String raw) {
|
||||
final percents = _bsdCpuPercentReg
|
||||
.allMatches(raw)
|
||||
.map((e) => double.parse(e.group(1) ?? '0') * 100)
|
||||
.toList();
|
||||
if (percents.length != 3) return initCpuStatus;
|
||||
return initCpuStatus
|
||||
..now = [
|
||||
OneTimeCpuStatus('cpu', percents[0].toInt(), percents[1].toInt(), 0,
|
||||
percents[2].toInt(), 0, 0, 0)
|
||||
];
|
||||
}
|
||||
|
||||
@@ -115,3 +115,51 @@ List<NetSpeedPart> parseNetSpeed(String raw, int time) {
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
/// [raw] example:
|
||||
/// Name Mtu Network Address Ipkts Ierrs Ibytes Opkts Oerrs Obytes Coll
|
||||
/// lo0 16384 <Link#1> 17296531 0 2524959720 17296531 0 2524959720 0
|
||||
/// lo0 16384 127 127.0.0.1 17296531 - 2524959720 17296531 - 2524959720 -
|
||||
/// lo0 16384 ::1/128 ::1 17296531 - 2524959720 17296531 - 2524959720 -
|
||||
/// lo0 16384 fe80::1%lo0 fe80:1::1 17296531 - 2524959720 17296531 - 2524959720 -
|
||||
/// gif0* 1280 <Link#2> 0 0 0 0 0 0 0
|
||||
/// stf0* 1280 <Link#3> 0 0 0 0 0 0 0
|
||||
/// en0 1500 <Link#4> 22:20:xx:xx:xx:e6 739447 0 693997876 535600 0 79008877 0
|
||||
/// en0 1500 fe80::f1:xx fe80:4::f1:xxxx:9 739447 - 693997876 535600 - 79008877 -
|
||||
/// en0 1500 192.168.2 192.168.2.111 739447 - 693997876 535600 - 79008877 -
|
||||
/// en0 1500 fd6b:xxxx:3 fd6b:xxxx:xxxx:0: 739447 - 693997876 535600 - 79008877 -
|
||||
/// en1 1500 <Link#5> 88:d8:xx:xx:xx:1d 0 0 0 0 0 0 0
|
||||
/// utun0 1380 <Link#6> 0 0 0 3 0 280 0
|
||||
/// utun0 1380 fe80::xxxx: fe80:6::xxxx:xxxx 0 - 0 3 - 280 -
|
||||
/// utun1 2000 <Link#7> 0 0 0 3 0 280 0
|
||||
/// utun1 2000 fe80::xxxx: fe80:7::xxxx:xxxx 0 - 0 3 - 280 -
|
||||
/// utun2 1000 <Link#8> 0 0 0 3 0 280 0
|
||||
/// utun2 1000 fe80::xxxx: fe80:8::xxxx:xxx: 0 - 0 3 - 280 -
|
||||
/// utun4 9000 <Link#10> 746744 0 845373390 386111 0 424400998 0
|
||||
/// utun4 9000 198.18.0/16 198.18.0.1 746744 - 845373390 386111 - 424400998 -
|
||||
/// en2* 1500 <Link#11> 36:7c:xx:xx:xx:xx 0 0 0 0 0 0 0
|
||||
List<NetSpeedPart> parseBsdNetSpeed(String raw, int time) {
|
||||
final split = raw.split('\n');
|
||||
if (split.length < 2) {
|
||||
return [];
|
||||
}
|
||||
|
||||
final results = <NetSpeedPart>[];
|
||||
for (final item in split.sublist(1)) {
|
||||
final data = item.trim().split(RegExp(r'\s+'));
|
||||
final device = data[0];
|
||||
if (device.endsWith('*')) {
|
||||
continue;
|
||||
}
|
||||
if (results.any((element) => element.device == device)) {
|
||||
continue;
|
||||
}
|
||||
if (data.length != 11) {
|
||||
continue;
|
||||
}
|
||||
final bytesIn = BigInt.parse(data[6]);
|
||||
final bytesOut = BigInt.parse(data[9]);
|
||||
results.add(NetSpeedPart(device, bytesIn, bytesOut, time));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'package:toolbox/data/model/server/system.dart';
|
||||
import 'package:toolbox/data/model/server/temp.dart';
|
||||
|
||||
import 'cpu.dart';
|
||||
@@ -16,6 +17,7 @@ class ServerStatus {
|
||||
Conn tcp;
|
||||
NetSpeed netSpeed;
|
||||
Temperatures temps;
|
||||
SystemType system;
|
||||
String? failedInfo;
|
||||
|
||||
ServerStatus({
|
||||
@@ -28,6 +30,7 @@ class ServerStatus {
|
||||
required this.netSpeed,
|
||||
required this.swap,
|
||||
required this.temps,
|
||||
required this.system,
|
||||
this.failedInfo,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'package:toolbox/data/model/server/system.dart';
|
||||
|
||||
import '../app/shell_func.dart';
|
||||
import 'cpu.dart';
|
||||
import 'disk.dart';
|
||||
@@ -9,11 +11,25 @@ import 'conn.dart';
|
||||
class ServerStatusUpdateReq {
|
||||
final ServerStatus ss;
|
||||
final List<String> segments;
|
||||
final SystemType system;
|
||||
|
||||
const ServerStatusUpdateReq(this.ss, this.segments);
|
||||
const ServerStatusUpdateReq({
|
||||
required this.system,
|
||||
required this.ss,
|
||||
required this.segments,
|
||||
});
|
||||
}
|
||||
|
||||
Future<ServerStatus> getStatus(ServerStatusUpdateReq req) async {
|
||||
switch (req.system) {
|
||||
case SystemType.linux:
|
||||
return _getLinuxStatus(req);
|
||||
case SystemType.bsd:
|
||||
return _getBsdStatus(req);
|
||||
}
|
||||
}
|
||||
|
||||
Future<ServerStatus> _getLinuxStatus(ServerStatusUpdateReq req) async {
|
||||
final segments = req.segments;
|
||||
|
||||
final time = int.parse(StatusCmdType.time.find(segments));
|
||||
@@ -24,7 +40,6 @@ Future<ServerStatus> getStatus(ServerStatusUpdateReq req) async {
|
||||
final sys = _parseSysVer(
|
||||
StatusCmdType.sys.find(segments),
|
||||
StatusCmdType.host.find(segments),
|
||||
StatusCmdType.sysRhel.find(segments),
|
||||
);
|
||||
if (sys != null) {
|
||||
req.ss.sysVer = sys;
|
||||
@@ -56,6 +71,30 @@ Future<ServerStatus> getStatus(ServerStatusUpdateReq req) async {
|
||||
return req.ss;
|
||||
}
|
||||
|
||||
Future<ServerStatus> _getBsdStatus(ServerStatusUpdateReq req) async {
|
||||
final segments = req.segments;
|
||||
|
||||
final time = int.parse(BSDStatusCmdType.time.find(segments));
|
||||
|
||||
final net = parseBsdNetSpeed(BSDStatusCmdType.net.find(segments), time);
|
||||
req.ss.netSpeed.update(net);
|
||||
|
||||
req.ss.sysVer = BSDStatusCmdType.sys.find(segments);
|
||||
|
||||
req.ss.cpu = parseBsdCpu(BSDStatusCmdType.cpu.find(segments));
|
||||
|
||||
//req.ss.mem = parseBsdMem(BSDStatusCmdType.mem.find(segments));
|
||||
|
||||
final uptime = _parseUpTime(BSDStatusCmdType.uptime.find(segments));
|
||||
if (uptime != null) {
|
||||
req.ss.uptime = uptime;
|
||||
}
|
||||
|
||||
req.ss.disk = parseDisk(BSDStatusCmdType.disk.find(segments));
|
||||
|
||||
return req.ss;
|
||||
}
|
||||
|
||||
// raw:
|
||||
// 19:39:15 up 61 days, 18:16, 1 user, load average: 0.00, 0.00, 0.00
|
||||
String? _parseUpTime(String raw) {
|
||||
@@ -69,17 +108,14 @@ String? _parseUpTime(String raw) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String? _parseSysVer(String raw, String hostname, String rawRhel) {
|
||||
String? _parseSysVer(String raw, String hostname) {
|
||||
try {
|
||||
final s = raw.split('=');
|
||||
if (s.length == 2) {
|
||||
return s[1].replaceAll('"', '').replaceFirst('\n', '');
|
||||
}
|
||||
} catch (e) {
|
||||
if (!rawRhel.contains('cat: /etc/redhat-release:')) {
|
||||
return rawRhel;
|
||||
}
|
||||
if (hostname.isNotEmpty) return hostname;
|
||||
} finally {
|
||||
// ignore: control_flow_in_finally
|
||||
return hostname.isEmpty ? null : hostname;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
34
lib/data/model/server/system.dart
Normal file
34
lib/data/model/server/system.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
import 'package:toolbox/data/model/app/shell_func.dart';
|
||||
|
||||
enum SystemType {
|
||||
linux._(linuxSign),
|
||||
bsd._(bsdSign),
|
||||
;
|
||||
|
||||
final String value;
|
||||
|
||||
const SystemType._(this.value);
|
||||
|
||||
static SystemType? parse(String? value) {
|
||||
if (value == null) return null;
|
||||
switch (value) {
|
||||
case linuxSign:
|
||||
return SystemType.linux;
|
||||
case bsdSign:
|
||||
return SystemType.bsd;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
bool isSegmentsLenMatch(int len) {
|
||||
switch (this) {
|
||||
case SystemType.linux:
|
||||
return len == StatusCmdType.values.length;
|
||||
case SystemType.bsd:
|
||||
return len == BSDStatusCmdType.values.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const linuxSign = 'linux';
|
||||
const bsdSign = 'bsd';
|
||||
@@ -1,5 +1,3 @@
|
||||
// ignore_for_file: prefer_final_fields
|
||||
|
||||
abstract class TimeSeq<T extends TimeSeqIface> {
|
||||
List<T> pre;
|
||||
List<T> now;
|
||||
|
||||
Reference in New Issue
Block a user