Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I hope to add process monitoring for the system #120

Open
kwrum1 opened this issue Oct 28, 2024 · 3 comments
Open

I hope to add process monitoring for the system #120

kwrum1 opened this issue Oct 28, 2024 · 3 comments

Comments

@kwrum1
Copy link

kwrum1 commented Oct 28, 2024

I hope to add process monitoring for the system.
I want to add a process monitor in the server monitor, next to docker

I hope that's how he works

processMonitor.ts
######################################
import { exec } from 'child_process';

export interface ProcessInfo {
pid: number;
name: string;
cpu: number;
memory: number;
}

const systemUsers = ['root', 'system', 'daemon'];

export const getProcessesInfo = async (): Promise<ProcessInfo[]> => {
return new Promise((resolve, reject) => {
exec('ps aux --sort=-%cpu,-rss', (error, stdout, stderr) => {
if (error) {
reject(exec error: ${error});
return;
}

  const processList: ProcessInfo[] = [];
  const lines = stdout.trim().split('\n');
  lines.shift(); // 移除标题行

  lines.forEach((line) => {
    const columns = line.trim().split(/\s+/);
    const user = columns[0];
    const pid = parseInt(columns[1], 10);
    const cpu = parseFloat(columns[2]);
    const memory = parseFloat(columns[3]);
    const name = columns.slice(10).join(' '); // 进程名称可能包含空格

    if (!systemUsers.includes(user)) {
      processList.push({ pid, name, cpu, memory });
    }
  });

  resolve(processList);
});

});
};
##########################
serverStatus.ts
##########################
import { Router, Request, Response } from 'express';
import { body, header, param } from 'express-validator';
import { validate } from '../middleware/validate.js';
import { recordServerStatus } from '../model/serverStatus.js';
import fs from 'fs-extra';
import { libraryPath } from '../utils/lib.js';
import { getProcessesInfo, ProcessInfo } from '../utils/processMonitor.js';

export const serverStatusRouter = Router();

const installScript = fs.readFileSync(libraryPath.installScript);

// 服务器状态报告路由
serverStatusRouter.post(
'/report',
validate(
header('x-tianji-report-version').isSemVer(),
body('workspaceId').isString(),
body('name').isString(),
body('hostname').isString(),
body('timeout').optional().isInt(),
body('payload').isObject()
),
async (req: Request, res: Response) => {
const body = req.body;
recordServerStatus(body);
res.send('success');
}
);

// 安装脚本路由
serverStatusRouter.get(
'/:workspaceId/install.sh',
validate(param('workspaceId').isString()),
async (req: Request, res: Response) => {
const { workspaceId } = req.params;
const queryUrl = req.query.url ? String(req.query.url) : undefined;
const server = queryUrl || ${req.protocol}://${req.get('Host')};
res
.setHeader('Content-Type', 'text/plain')
.send(
String(installScript)
.replace('{{DEFAULT_SERVER}}', server)
.replace('{{DEFAULT_WORKSPACE}}', workspaceId)
);
}
);

// 进程监控路由
serverStatusRouter.get(
'/process-monitor',
async (req: Request, res: Response) => {
try {
const processes: ProcessInfo[] = await getProcessesInfo();
res.json(processes);
} catch (err) {
res.status(500).json({ error: '获取进程信息失败' });
}
}
);
#############################

@moonrailgun
Copy link
Contributor

i think its cool, did you wanna submit a PR for it?

@moonrailgun
Copy link
Contributor

its better for display top 3, more is unnecessary.

@kwrum1
Copy link
Author

kwrum1 commented Nov 1, 2024

最好显示前 3 名,没有必要显示更多。
From a usability perspective, I would like to customize the selection of process monitoring, such as when my server is running "Home Assistant," I can know when its process is killed, so that I can immediately maintain it.

and,Developing JavaTypeScript has been challenging for me, and I apologize that I am unable to fix the issue and submit a PR. However, I can provide you with a possible solution and reference code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants