nx_fleet_doctor.nx source
↩ module page · 67 lines · 3717 B
1// nx_fleet_doctor.nx -- the self-healing fleet capstone. Extends the local
2// auto-fix doctor with the REMOTE arm the operator originally asked for:
3// "autofix these issues if they are local, or report and the network fix
4// them automatically if they are remote." The remote fix is now REAL --
5// driven over our OWN sovereign SSH (passwordless, Ed25519): the doctor
6// reaches a remote node, checks a service, and auto-heals it if down.
7//
8// Composes nx_ssh_lib (sovereign SSH) + the doctor's probe/classify pattern.
9// Bits-up: our own crypto + raw syscalls, no openssh.
10//
11// license_tier: ORIGINAL
12
13import "nx_syscalls.nx"
14import "nx_connect.nx" // bounded connect: a raw sys_connect hangs ~127s on a black-holed host
15import "nx_ssh_lib.nx"
16const K_MAGIC_8787: i64 = 8787
17
18func slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != 0 as u8 { n = n + 1 } return n }
19
20// local TCP reachability probe (1s timeout).
21func fd_tcp_ok(ip: i64, port: i64) -> i64 {
22 let fd: i64 = sys_socket(AF_INET, SOCK_STREAM, 0)
23 if fd < 0 { return 0 }
24 let tv: *u8 = sys_mmap(16); tv[0] = 1 as u8
25 var z: i64 = 1
26 while z < 16 { tv[z] = 0 as u8; z = z + 1 }
27 sys_setsockopt(fd, SOL_SOCKET, 21, tv, 16) // SO_SNDTIMEO
28 let sa: *u8 = sys_mmap(16); ssh_sockaddr(sa, ip, port)
29 let rc: i64 = nx_connect_bounded(fd, sa, 16, NX_CONN_DEFAULT_MS)
30 sys_close(fd)
31 if rc == 0 { return 1 }
32 return 0
33}
34
35func main() -> i64 {
36 ssh_puts("NISHI FLEET DOCTOR -- local auto-fix + REMOTE self-heal (over our own SSH)\n" as *u8)
37 ssh_puts("------------------------------------------------------------------------\n" as *u8)
38
39 // ---- LOCAL: search service (auto-fixed locally by nx_doctor) ----
40 ssh_puts(" [local] search 127.0.0.1:8787 .......... " as *u8)
41 if fd_tcp_ok((127 << 24) | 1, K_MAGIC_8787) == 1 { ssh_puts("[OK] up\n" as *u8) }
42 else { ssh_puts("[!!] DOWN -- run nx_doctor to auto-fix locally\n" as *u8) }
43
44 // ---- REMOTE: NAS sites daemon, reached + self-healed via sovereign SSH ----
45 ssh_puts(" [remote] NAS sites daemon :8443 ......... reaching via sovereign SSH...\n" as *u8)
46 let seed: *u8 = sys_mmap(32); let pub: *u8 = sys_mmap(32)
47 ssh_load_identity(seed, pub)
48 let st: *SshState = sys_mmap(SSH_STATE_BYTES) as *SshState
49 if ssh_open_session(st, (192 << 24) | (168 << 16) | (8 << 8) | 240) != 0 {
50 ssh_puts(" [XX] SSH session failed (node unreachable?)\n" as *u8); return 1
51 }
52 if ssh_userauth_publickey(st, "elderwesto" as *u8, 10, seed, pub) != 1 {
53 ssh_puts(" [XX] publickey auth failed (our key not authorized on the node)\n" as *u8)
54 sys_close(st.fd); return 2
55 }
56 ssh_puts(" authenticated passwordless (Ed25519, our crypto). health + heal:\n ----\n" as *u8)
57 // check :8443; if DOWN, restart sites.elf and re-verify -- the remote auto-fix.
58 let cmd: *u8 = "if netstat -tln 2>/dev/null | grep -q ':8443 '; then echo ' REMOTE_SITES_UP (healthy, no action)'; else echo ' REMOTE_SITES_DOWN -- AUTO-HEALING'; cd /volume1/homes/elderwesto/nishihost && setsid ./sites.elf >> sites.elf.log 2>&1 < /dev/null & sleep 2; netstat -tln 2>/dev/null | grep -q ':8443 ' && echo ' HEALED' || echo ' STILL_DOWN'; fi" as *u8
59 ssh_exec(st, cmd, slen(cmd))
60 ssh_puts(" ----\n" as *u8)
61 sys_close(st.fd)
62
63 ssh_puts("\n Summary: local services probed (auto-fixable here); the REMOTE node was\n" as *u8)
64 ssh_puts(" reached + its service health-checked + self-heal armed -- all over our OWN\n" as *u8)
65 ssh_puts(" bits-up SSH. Realizes: autofix if local, the network heals it if remote.\n" as *u8)
66 return 0
67}