-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.ts
80 lines (74 loc) · 1.82 KB
/
reader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const files: Array<string> = [
"/proc/meminfo", // Memory stats
"/proc/cpuinfo", // CPU info
"/proc/{pid}/smaps", // Pid maps (memory information)
"/proc/{pid}/exe", // The symlink of process binary
];
export function readProcDir(): Array<string> {
try {
const tmp: Array<string> = [];
for (const dir of Deno.readDirSync("/proc")) {
if (dir.isDirectory && parseInt(dir.name)) {
tmp.push(dir.name);
}
}
return tmp;
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
// console.log(error);
return [];
}
}
return []
}
export function readMeminfo(): string {
try {
const decoder = new TextDecoder();
return decoder.decode(Deno.readFileSync(files[0]));
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
// console.log(error);
return "";
}
}
return ""
}
export function readCpuinfo(): string {
try {
const decoder = new TextDecoder();
return decoder.decode(Deno.readFileSync(files[1]));
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
// console.log(error);
return "";
}
}
return ""
}
export function readPidSmaps(pid: string): string {
try {
const statPath = files[2].replace("{pid}", pid);
const decoder = new TextDecoder();
const file = Deno.readFileSync(statPath);
return decoder.decode(file);
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
// console.log(error);
return "";
}
}
return "";
}
export function readPidBinarySymlink(pid: number): string {
try {
const path = files[4].replace("{pid}", pid)
const pidBinary = Deno.readLinkSync(path)
return pidBinary;
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
// console.log(error);
return "";
}
}
return ""
}