mirror of
https://github.com/pockethost/pockethost.git
synced 2025-06-07 14:46:41 +00:00
feat(pockethost): lsof command
This commit is contained in:
parent
1d475927e9
commit
d687fcf98a
45
packages/pockethost/src/cli/display.ts
Normal file
45
packages/pockethost/src/cli/display.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import { execSync } from 'child_process'
|
||||||
|
import fs, { writeFileSync } from 'fs'
|
||||||
|
|
||||||
|
// Define the structure of the input JSON
|
||||||
|
interface Process {
|
||||||
|
command: string
|
||||||
|
ppid: number | null
|
||||||
|
pid: number
|
||||||
|
tid: number | null
|
||||||
|
taskcmd: string | null
|
||||||
|
user: string
|
||||||
|
fd: string
|
||||||
|
type: string
|
||||||
|
device: string
|
||||||
|
size_off: number
|
||||||
|
node: number
|
||||||
|
name: string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load and parse the JSON file
|
||||||
|
const data: Process[] = JSON.parse(fs.readFileSync('lsof.json', 'utf-8'))
|
||||||
|
|
||||||
|
const cached: { [_: string]: number | null } = {}
|
||||||
|
data.forEach((proc) => {
|
||||||
|
const { pid } = proc
|
||||||
|
const ppid = (() => {
|
||||||
|
if (pid in cached) {
|
||||||
|
return cached[pid]!
|
||||||
|
}
|
||||||
|
console.log(`miss. ${pid} is not in cache`)
|
||||||
|
try {
|
||||||
|
const psOutput = execSync(`ps -o ppid= -p ${proc.pid}`)
|
||||||
|
const ppid = parseInt(psOutput.toString().trim(), 10)
|
||||||
|
cached[pid] = ppid
|
||||||
|
return ppid
|
||||||
|
} catch (e) {
|
||||||
|
cached[pid] = null
|
||||||
|
}
|
||||||
|
return cached[pid]!
|
||||||
|
})()
|
||||||
|
proc.ppid = ppid
|
||||||
|
console.log(`${pid}->${ppid}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
writeFileSync(`./lsof.json`, JSON.stringify(data, null, 2))
|
790909
packages/pockethost/src/cli/lsof.json
Normal file
790909
packages/pockethost/src/cli/lsof.json
Normal file
File diff suppressed because it is too large
Load Diff
71
packages/pockethost/src/cli/lsof.ts
Normal file
71
packages/pockethost/src/cli/lsof.ts
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
import { map } from '@s-libs/micro-dash'
|
||||||
|
import { execSync } from 'child_process'
|
||||||
|
import fs from 'fs'
|
||||||
|
|
||||||
|
// Define the structure of the input JSON
|
||||||
|
export interface Process {
|
||||||
|
command: string
|
||||||
|
ppid: number | null
|
||||||
|
pid: number
|
||||||
|
tid: number | null
|
||||||
|
taskcmd: string | null
|
||||||
|
user: string
|
||||||
|
fd: string
|
||||||
|
type: string
|
||||||
|
device: string
|
||||||
|
size_off: number
|
||||||
|
node: number
|
||||||
|
name: string
|
||||||
|
childCount: number
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load and parse the JSON file
|
||||||
|
const data: Process[] = JSON.parse(fs.readFileSync('lsof.json', 'utf-8'))
|
||||||
|
|
||||||
|
const rollup = () => {
|
||||||
|
const cached: { [_: string]: number } = {}
|
||||||
|
data.forEach((proc) => {
|
||||||
|
const { command } = proc
|
||||||
|
if (!cached[command]) cached[command] = 0
|
||||||
|
cached[command]++
|
||||||
|
})
|
||||||
|
|
||||||
|
map(cached, (v, k) => {
|
||||||
|
return { v, k }
|
||||||
|
})
|
||||||
|
.filter(({ v, k }) => {
|
||||||
|
return v > 1000
|
||||||
|
})
|
||||||
|
.sort((a, b) => {
|
||||||
|
return b.v - a.v
|
||||||
|
})
|
||||||
|
.forEach(({ v, k }) => {
|
||||||
|
console.log(`${k}: ${v}`)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const parent = () => {
|
||||||
|
const cached: { [_: string]: number | null } = {}
|
||||||
|
data.forEach((proc) => {
|
||||||
|
const { pid } = proc
|
||||||
|
const ppid = (() => {
|
||||||
|
if (pid in cached) {
|
||||||
|
return cached[pid]!
|
||||||
|
}
|
||||||
|
console.log(`miss. ${pid} is not in cache`)
|
||||||
|
try {
|
||||||
|
const psOutput = execSync(`ps -o ppid= -p ${proc.pid}`)
|
||||||
|
const ppid = parseInt(psOutput.toString().trim(), 10)
|
||||||
|
cached[pid] = ppid
|
||||||
|
return ppid
|
||||||
|
} catch (e) {
|
||||||
|
cached[pid] = null
|
||||||
|
}
|
||||||
|
return cached[pid]!
|
||||||
|
})()
|
||||||
|
proc.ppid = ppid
|
||||||
|
console.log(`${pid}->${ppid}`)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
rollup()
|
Loading…
x
Reference in New Issue
Block a user