code wiki / _hdl_build / nx_health_monitor.nx

nx_health_monitor.nx source

↩ module page · 98 lines · 5496 B

1// nx_health_monitor.nx -- LIVE true-monitor: fetch each endpoint in health_templates.conf and validate the 2// actual CONTENT against its known-good template (nx_health_template) -- NOT just the HTTP status. Operator: 3// "check against known good templates -- a 404 being 'up' with a 200 status is wrong." This is the systemic fix 4// for the false-greens: a 200-with-error-content, a wrong/blank page, or a non-200 are all reported DOWN with the 5// specific reason. Composes the sovereign fetch (nx_research_fetch -> status + body to knowledge/fetched/ 6// srch_latest.raw) + the gated htpl_verdict. Run: nx_health_monitor [conf]. license_tier: ORIGINAL 7import "nx_health_template.nx" 8import "nx_http_health_lib.nx" // brings dep_run_capture + dp_read + hh_after (+ nx_deploy_lib) 9const HM_MAGIC_1024: i64 = 1024 10const HM_MAGIC_8192: i64 = 8192 11const HM_MAGIC_2097152: i64 = 2097152 12const HM_MAGIC_8191: i64 = 8191 13 14const HM_BODY: *u8 = "knowledge/fetched/srch_latest.raw" as *u8 // where nx_research_fetch saves the body 15const HM_FETCH_ELF: *u8 = "_offc/nx_research_fetch.elf" as *u8 16 17func hm_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 18func hm_eol(b: *u8, n: i64, st: i64) -> i64 { var i: i64=st; var f: i64=0; while f==0 { if i>=n {f=1} else { if (b[i] as i64)==10 {f=1} else {i=i+1} } } return i } 19func hm_verdict_name(v: i64) -> i64 { 20 if v == HT_OK { hm_w("HEALTHY" as *u8) } 21 if v == HT_WRONG_STATUS { hm_w("DOWN (wrong-status / fetch-failed)" as *u8) } 22 if v == HT_ERROR_CONTENT { hm_w("DOWN (200-but-ERROR-CONTENT -- the false-green)" as *u8) } 23 if v == HT_MISSING_TEMPLATE { hm_w("DOWN (200 but NOT the known-good template)" as *u8) } 24 return 0 25} 26 27func main(argc: i64, argv: *i64) -> i64 { 28 var conf: *u8 = "knowledge/hosting/health_templates.conf" as *u8 29 if argc >= 2 { conf = argv[1] as *u8 } 30 let szb: *i64 = sys_mmap(16) as *i64 31 szb[0] = 0 32 let cfg: *u8 = sys_read_file(conf, szb) 33 if (cfg as i64) == 0 { hm_w("health-monitor: cannot read " as *u8); hm_w(conf); hm_w("\n" as *u8); return 1 } 34 let n: i64 = szb[0] 35 36 hm_w("=== NISHI LIVE HEALTH MONITOR (content-aware: a 200 with 404/error content is DOWN) ===\n" as *u8) 37 let url: *u8 = sys_mmap(HM_MAGIC_1024) 38 let marker: *u8 = sys_mmap(256) 39 let fout: *u8 = "/tmp/nx_hm_fetch.out" as *u8 40 let fbuf: *u8 = sys_mmap(HM_MAGIC_8192) 41 let body: *u8 = sys_mmap(HM_MAGIC_2097152) 42 var cur: i64 = 0 43 var healthy: i64 = 0 44 var down: i64 = 0 45 while cur < n { 46 let le: i64 = hm_eol(cfg, n, cur) 47 // skip comments + blanks 48 var ok_line: i64 = 1 49 if le <= cur { ok_line = 0 } 50 if ok_line == 1 { if (cfg[cur] as i64) == 35 { ok_line = 0 } } 51 if ok_line == 1 { 52 // split url <TAB> marker 53 var tab: i64 = 0 - 1 54 var i: i64 = cur 55 while i < le { if (cfg[i] as i64) == 9 { tab = i; i = le } else { i = i + 1 } } 56 var uend: i64 = le 57 if tab >= 0 { uend = tab } 58 // url (trim trailing spaces) 59 var ul: i64 = 0 60 var k: i64 = cur 61 while k < uend { url[ul] = cfg[k]; ul = ul + 1; k = k + 1 } 62 while ul > 0 { if (url[ul-1] as i64) == 32 { ul = ul - 1 } else { url[ul] = 0 as u8; ul = 0 - ul } } 63 if ul <= 0 { ul = 0 - ul } 64 url[ul] = 0 as u8 65 // marker 66 var ml: i64 = 0 67 if tab >= 0 { var m: i64 = tab + 1; while m < le { marker[ml] = cfg[m]; ml = ml + 1; m = m + 1 } } 68 // trim trailing CR/space from marker 69 while ml > 0 { let c: i64 = marker[ml-1] as i64; if c == 13 { ml = ml - 1 } else { if c == 32 { ml = ml - 1 } else { ml = 0 - (ml + 1) } } } 70 if ml < 0 { ml = (0 - ml) - 1 } 71 marker[ml] = 0 as u8 72 73 if ul > 0 { 74 // FETCH (sovereign): status to fout, body to HM_BODY 75 let fargs: *i64 = sys_mmap(16) as *i64 76 fargs[0] = url as i64 77 dep_run_capture(HM_FETCH_ELF, fargs, 1, fout) 78 let fn: i64 = dp_read(fout, fbuf, HM_MAGIC_8191) 79 let status: i64 = hh_after(fbuf, fn, "status=" as *u8) 80 let bsz: *i64 = sys_mmap(16) as *i64 81 bsz[0] = 0 82 let bp: *u8 = sys_read_file(HM_BODY, bsz) 83 var bn: i64 = 0 84 if (bp as i64) != 0 { bn = bsz[0]; var z: i64 = 0; while z < bn { if z < HM_MAGIC_2097152 { body[z] = bp[z] } z = z + 1 } } 85 let v: i64 = htpl_verdict(body, bn, status, marker, ml) 86 if v == HT_OK { healthy = healthy + 1 } else { down = down + 1 } 87 hm_w(" " as *u8); hm_w(url); hm_w(" -> " as *u8); hm_verdict_name(v); hm_w("\n" as *u8) 88 } 89 } 90 cur = le + 1 91 } 92 hm_w("----\n healthy=" as *u8) 93 let db: *u8 = sys_mmap(16); var hv: i64 = healthy; var hk: i64 = 0; if hv == 0 { db[0]=48 as u8; hk=1 } else { while hv>0 { db[hk]=(48+(hv%10)) as u8; hv=hv/10; hk=hk+1 } } var hx: i64 = hk-1; while hx>=0 { sys_write(1, ((db as i64)+hx) as *u8, 1); hx=hx-1 } 94 hm_w(" down=" as *u8) 95 let d2: *u8 = sys_mmap(16); var dv: i64 = down; var dk: i64 = 0; if dv == 0 { d2[0]=48 as u8; dk=1 } else { while dv>0 { d2[dk]=(48+(dv%10)) as u8; dv=dv/10; dk=dk+1 } } var dx: i64 = dk-1; while dx>=0 { sys_write(1, ((d2 as i64)+dx) as *u8, 1); dx=dx-1 } 96 hm_w(" (content-validated, not just status)\n" as *u8) 97 return 0 98}