CH-J SM Mobileуправление на сървъри над SSH
Меню
Публикуван източник

CH-J SM Mobile

Преглед на директории и файлове за конкретна версия на приложението.

Изтегляне на източник ZIP
CH-J / SM Mobile 134 Файлове
sm-mobile-20261016T050549Z-3678f2a05fa9 Добавено: · Построена:
Файлове
monitor.dart Добавено: Дата, която не е регистрирана 3,2 KB · 107 линии
Изтегляне на файл
1import 'dart:async';
2import 'dart:convert';
3import 'package:dartssh2/dartssh2.dart';
4import '../core/failure.dart';
6abstract interface class MonitorAdapter {
7 Future<Map<String, String>> sample(SSHClient client);
8 void cancel();
9}
11class LinuxMonitor implements MonitorAdapter {
12 final Set<SSHSession> _active = {};
13 int _epoch = 0;
14 @override
15 void cancel() {
16 _epoch++;
17 for (final session in _active) {
18 session.close();
19 }
20 _active.clear();
21 }
23 Future<SSHSession> _execute(
24 SSHClient client,
25 String command,
26 int epoch,
27 ) async {
28 return client
29 .execute(command)
30 .then((session) {
31 if (epoch != _epoch) {
32 session.close();
33 throw const Failure('cancelled');
34 }
35 _active.add(session);
36 return session;
37 })
38 .timeout(
39 const Duration(seconds: 10),
40 onTimeout: () {
41 cancel();
42 throw TimeoutException('SSH channel');
43 },
44 );
45 }
47 static const command = r'''LC_ALL=C; export LC_ALL
48test "$(uname -s)" = Linux || exit 70
49printf 'system='; uname -sr
50printf 'release='; sed -n 's/^PRETTY_NAME=//p' /etc/os-release
51printf 'uptime='; cut -d ' ' -f 1 /proc/uptime
52printf 'load='; cut -d ' ' -f 1-3 /proc/loadavg
53awk '/^MemTotal:/{t=$2}/^MemAvailable:/{a=$2}END{if(t>0)printf "ram=%.1f%% (%d / %d MiB)\n",100*(t-a)/t,(t-a)/1024,t/1024}' /proc/meminfo
54df -Pk / | awk 'NR==2{printf "disk=%s (%s / %s KiB)\n",$5,$3,$2}'
55a=$(awk '/^cpu /{t=0;for(i=2;i<=9;i++)t+=$i;printf "%.0f %.0f\n",t,$5+$6;exit}' /proc/stat)
56sleep 1
57b=$(awk '/^cpu /{t=0;for(i=2;i<=9;i++)t+=$i;printf "%.0f %.0f\n",t,$5+$6;exit}' /proc/stat)
58awk -v a="$a" -v b="$b" 'BEGIN{split(a,x," ");split(b,y," ");t=y[1]-x[1];if(t>0)printf "cpu=%.1f%%\n",100*(t-y[2]+x[2])/t}'
59''';
60 @override
61 Future<Map<String, String>> sample(SSHClient client) async {
62 final epoch = _epoch;
63 final watch = Stopwatch()..start();
64 final ping = await _execute(client, 'true', epoch);
65 try {
66 await ping.done.timeout(const Duration(seconds: 10));
67 } finally {
68 ping.close();
69 _active.remove(ping);
70 }
71 final rtt = watch.elapsedMilliseconds;
72 final session = await _execute(client, command, epoch);
73 final output = StringBuffer();
74 var bytes = 0;
75 final sub = session.stdout
76 .cast<List<int>>()
77 .transform(const Utf8Decoder(allowMalformed: true))
78 .listen((s) {
79 bytes += s.length;
80 if (bytes <= 65536) {
81 output.write(s);
82 } else {
83 session.close();
84 }
85 });
86 final err = session.stderr.listen((_) {});
87 try {
88 await session.done.timeout(const Duration(seconds: 15));
89 if (session.exitCode != 0 || bytes > 65536) {
90 throw const Failure('unsupportedSystem');
91 }
92 return {
93 'rtt': '$rtt ms',
94 for (final line in output.toString().split('\n'))
95 if (line.contains('='))
96 line.substring(0, line.indexOf('=')): line.substring(
97 line.indexOf('=') + 1,
98 ),
99 };
100 } finally {
101 await sub.cancel();
102 await err.cancel();
103 session.close();
104 _active.remove(session);
105 }
106 }
SHA-25688d23fd89e0631ecbcc92bdd3771193581dcd9b793b80f47ccae946124784256

CH-J Proprietary Software License 1.14 · Източникът е предоставен под CH-J Proprietary Software License 1.14. Неговата наличност не променя лицензионните условия или не предоставя допълнителни права.

Архив SHA-256c486a807e4828998b9c57ab0f2e760f7980b31965fe118e89d3e1a5a2b65be65