dsh-ops-kit 0.3.3 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/client.js CHANGED
@@ -526,6 +526,11 @@ window.__ModuleLoader__.load({
526
526
  setBusy('logs')
527
527
  try {
528
528
  const data = await api(`tail?host=${encodeURIComponent(cHost || hosts[0]?.name)}&source=${encodeURIComponent(source)}&lines=${logLines}`, {}, timeoutFor('action'))
529
+ if (data.missing) {
530
+ setLogText(data.error || '')
531
+ void loadInv(cHost || hosts[0]?.name, true)
532
+ return
533
+ }
529
534
  const head = data.resolved ? `# ${data.via === 'journal' ? 'journalctl -u ' + data.resolved : data.resolved}\n\n` : ''
530
535
  setLogText(head + (data.lines || data.error || ''))
531
536
  } catch (e) { setLogText(String(e.message || e)) } finally { setBusy('') }
@@ -1081,7 +1086,7 @@ window.__ModuleLoader__.load({
1081
1086
  disabled: busy === `act:${app.kind}:${app.name}:logs`,
1082
1087
  onClick: () => {
1083
1088
  if (app.kind === 'java') {
1084
- const src = `pid:${String(app.name).replace(/[^0-9]/g, '')}`
1089
+ const src = app.jar ? `jar:${app.jar}` : `pid:${String(app.pid || app.name).replace(/[^0-9]/g, '')}`
1085
1090
  setLogSource(src)
1086
1091
  setDetailTab('logs')
1087
1092
  void loadLogs(src)
package/docs/USAGE.md CHANGED
@@ -101,6 +101,8 @@ Click a host from the overview; the left side has ten sub-pages:
101
101
 
102
102
  **You do not have to remember log sources**: clicking the source field opens a list — a systemd unit (`nginx.service`), a java process (`pid:3154754`), or a log file (`file:/var/log/x.log`). For a java process the plugin finds the log itself: `journalctl -u` when the process belongs to a unit, otherwise the newest `.log` / `.out` file it has open. The resolved source is shown above the output.
103
103
 
104
+ **A java app restarts, so its pid changes**: clicking Logs on a java row uses `jar:<jar file>` instead — it looks up the current pid from the command line, so it keeps working after a restart. If you type a `pid:12345` that no longer exists, the page says so explicitly and rescans the inventory for you.
105
+
104
106
  **Restart, stop, restore and rollback all ask for confirmation first.**
105
107
 
106
108
  ---
@@ -88,6 +88,8 @@ ops_check_all
88
88
 
89
89
  **日志来源不用记**:点来源输入框会弹出列表 —— systemd 服务名(`nginx.service`)、java 进程(`pid:3154754`)、或日志文件(`file:/var/log/xxx.log`)。选 java 进程时插件自己找日志:该进程属于某个服务就用 `journalctl -u`,否则取它打开的最新的 `.log` / `.out` 文件。读取结果顶部会标出实际用的来源。
90
90
 
91
+ **java 应用会重启,pid 会变**:所以在「应用」页点 java 进程的「日志」时,插件优先用 `jar:<jar 文件名>` 定位(按命令行反查当前 pid,重启后仍然有效)。如果你手动填了 `pid:12345` 而那个进程已经没了,页面会明确告诉你,并自动重新扫描清单。
92
+
91
93
  **重启 / 停止 / 回滚 / 恢复都会先弹确认**,不会点了就执行。
92
94
 
93
95
  ---
package/index.js CHANGED
@@ -1362,7 +1362,8 @@ async function discover(cfg) {
1362
1362
  ...docker.map(row => ({ kind: 'docker', name: row.name, status: row.status, ports: row.ports, config: null })),
1363
1363
  ...services.filter(name => !/^(systemd-|dbus|cron|crond|sshd|rsyslog|polkit|NetworkManager|auditd|tuned|chronyd|irqbalance|atd|rngd|rpcbind|gssproxy|firewalld|BT-FirewallServices|aegis|aliyun|cloudmonitor|getty|serial-getty|user@|postfix)/.test(name))
1364
1364
  .map(name => ({ kind: 'service', name, status: 'running', ports: '', config: configFor(name) })),
1365
- ...java.map(row => ({ kind: 'java', name: `java pid ${row.pid}`, status: row.uptime, ports: '', config: null })),
1365
+ ...java.map(row => ({ kind: 'java', name: `java pid ${row.pid}`, status: row.uptime, ports: '', config: null,
1366
+ pid: row.pid, jar: String(row.cmd || '').replace(/^.*-jar\s+/, '').replace(/\s.*$/, '').replace(/^.*\//, '') })),
1366
1367
  ]
1367
1368
  const portRows = lines('ports').filter(line => /^LISTEN/.test(line)).map(line => {
1368
1369
  const portMatch = line.match(/(?:\d{1,3}\.){3}\d{1,3}:(\d+)|\*:(\d+)|:(\d+)\s/)
@@ -1669,7 +1670,7 @@ async function killProcess(cfg, pid, signal = 'TERM') {
1669
1670
  async function tailLog(cfg, source, lines = 200) {
1670
1671
  const n = Math.min(Math.max(Number(lines) || 200, 20), 1000)
1671
1672
  const raw = String(source || '')
1672
- if (raw.startsWith('pid:')) return await tailProcessLog(cfg, Number(raw.slice(4)), n)
1673
+ if (raw.startsWith('pid:') || raw.startsWith('jar:')) return await tailProcessLog(cfg, raw, n)
1673
1674
  let command
1674
1675
  if (raw.startsWith('file:')) {
1675
1676
  const path = raw.slice(5)
@@ -1688,18 +1689,43 @@ async function tailLog(cfg, source, lines = 200) {
1688
1689
  * Uses the owning systemd unit when there is one, otherwise tails the newest
1689
1690
  * .log/.out file the process actually has open.
1690
1691
  */
1691
- async function tailProcessLog(cfg, pid, lines = 200) {
1692
+ async function tailProcessLog(cfg, source, lines = 200) {
1692
1693
  const n = Math.min(Math.max(Number(lines) || 200, 20), 1000)
1693
- if (!Number.isInteger(pid) || pid <= 0) throw new Error('a valid pid is required')
1694
+ const raw = String(source || '')
1694
1695
 
1695
- const unitRes = await ssh(cfg, `ps -o unit= -p ${pid} 2>/dev/null | tr -d ' \n'; true`, 20_000)
1696
- const unit = String(unitRes.stdout || '').trim()
1697
- if (unit && unit !== '-' && !unit.startsWith('session-') && !unit.includes(' ')) {
1698
- const res = await ssh(cfg, `journalctl -u ${unit} -n ${n} --no-pager 2>&1`, 25_000)
1696
+ let pid = 0
1697
+ if (raw.startsWith('jar:')) {
1698
+ const jar = raw.slice(4).trim()
1699
+ if (!/^[A-Za-z0-9._@/-]+$/.test(jar)) throw new Error('bad jar name')
1700
+ const found = await ssh(cfg, `for p in $(pgrep -f -- ${JSON.stringify(jar)} 2>/dev/null); do c=$(tr '\\0' ' ' < /proc/$p/cmdline 2>/dev/null); case "$c" in *java*) echo "$p"; break;; esac; done`, 20_000)
1701
+ pid = Number(String(found.stdout || '').trim())
1702
+ if (!pid) {
1703
+ return {
1704
+ host: cfg.name, source: raw, resolved: '', via: 'none', missing: true, ok: false, lines: '',
1705
+ error: `没有找到正在运行 ${jar} 的进程。应用可能没在跑,或启动方式已变(不再带 -jar)。到「应用」页点「重新扫描」看看当前进程。`,
1706
+ }
1707
+ }
1708
+ } else {
1709
+ pid = Number(raw.slice(4))
1710
+ if (!Number.isInteger(pid) || pid <= 0) throw new Error('a valid pid is required')
1711
+ }
1712
+
1713
+ // One round trip: an existing pid's unit, or a marker when the pid is gone.
1714
+ const probe = await ssh(cfg, `if [ -d /proc/${pid} ]; then ps -o unit= -p ${pid} 2>/dev/null | tr -d ' \\n'; else echo '__GONE__'; fi`, 20_000)
1715
+ const probed = String(probe.stdout || '').trim()
1716
+ if (probed === '__GONE__') {
1699
1717
  return {
1700
- host: cfg.name, source: `pid:${pid}`, resolved: unit, via: 'journal',
1701
- ok: res.ok, lines: String(res.stdout || '').slice(0, 60_000),
1702
- error: res.ok ? '' : cleanStderr(res.stderr),
1718
+ host: cfg.name, source: raw, resolved: '', via: 'none', missing: true, ok: false, lines: '',
1719
+ error: `PID ${pid} 已经不存在了(应用可能重启过)。到「应用」页点「重新扫描」取最新的进程;如果这个应用会重启,改用 jar 名定位更稳。`,
1720
+ }
1721
+ }
1722
+
1723
+ if (probed && probed !== '-' && !probed.startsWith('session-') && !probed.includes(' ')) {
1724
+ const res = await ssh(cfg, `journalctl -u ${probed} -n ${n} --no-pager 2>&1`, 25_000)
1725
+ const body = String(res.stdout || '')
1726
+ const meaningful = body.split('\n').filter(line => line.trim() && !line.startsWith('--')).length > 0
1727
+ if (res.ok && meaningful) {
1728
+ return { host: cfg.name, source: raw, resolved: probed, via: 'journal', ok: true, lines: body.slice(0, 60_000), error: '' }
1703
1729
  }
1704
1730
  }
1705
1731
 
@@ -1711,9 +1737,8 @@ async function tailProcessLog(cfg, pid, lines = 200) {
1711
1737
  const files = String(scan.stdout || '').split('\n').map(s => s.trim()).filter(f => f.startsWith('/'))
1712
1738
  if (files.length === 0) {
1713
1739
  return {
1714
- host: cfg.name, source: `pid:${pid}`, resolved: '', via: 'none',
1715
- ok: false, lines: '',
1716
- error: `PID ${pid} 既不是 systemd 服务,也没有打开 .log / .out 文件。请到「文件」页找到日志路径,再在来源里填 file:/var/log/xxx.log`,
1740
+ host: cfg.name, source: raw, resolved: '', via: 'none', ok: false, lines: '',
1741
+ error: `PID ${pid} 不是 systemd 服务,也没有打开 .log / .out 文件。请到「文件」页找到日志路径,再在来源里填 file:/var/log/xxx.log`,
1717
1742
  }
1718
1743
  }
1719
1744
 
@@ -1722,7 +1747,7 @@ async function tailProcessLog(cfg, pid, lines = 200) {
1722
1747
  const picked = String(newest.stdout || '').split('\n')[0].trim() || files[0]
1723
1748
  const res = await ssh(cfg, `tail -n ${n} ${JSON.stringify(picked)} 2>&1`, 25_000)
1724
1749
  return {
1725
- host: cfg.name, source: `pid:${pid}`, resolved: picked, via: 'file',
1750
+ host: cfg.name, source: raw, resolved: picked, via: 'file',
1726
1751
  ok: res.ok, lines: String(res.stdout || '').slice(0, 60_000),
1727
1752
  error: res.ok ? '' : cleanStderr(res.stderr),
1728
1753
  }
@@ -3341,10 +3366,10 @@ export function apply(ctx) {
3341
3366
 
3342
3367
  asTool({
3343
3368
  name: 'ops_logs',
3344
- description: 'Last N lines from a systemd unit or a log file on a host. Use source "nginx.service" or "file:/var/log/nginx/error.log".',
3369
+ description: 'Last N lines from a systemd unit, a log file, a bare pid or a java app. Use source "nginx.service", "file:/var/log/nginx/error.log", "pid:1234" for one process, or "jar:app.jar" to follow a java app across restarts.',
3345
3370
  parameters: {
3346
3371
  host: { type: 'string', description: 'host name from ops_hosts' },
3347
- source: { type: 'string', description: 'unit name, or file:/absolute/path' },
3372
+ source: { type: 'string', description: 'unit name, file:/absolute/path, pid:<pid>, or jar:<jar name>' },
3348
3373
  lines: { type: 'number', description: 'line count; defaults to 200' },
3349
3374
  },
3350
3375
  async execute(args = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-ops-kit",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "description": "Server operations kit for DeepSeek Harness: server cards, health charts, config browser, app manager and plain-language AI diagnosis — over SSH, no agent install.",
5
5
  "license": "MIT",
6
6
  "type": "module",