1. IT系统工具开发概述
IT系统工具开发是指开发用于IT系统管理、监控、维护和优化的工具,包括系统监控工具、自动化工具、备份工具、安全管理工具等多个方面。IT系统工具可以提高运维效率,减少人工操作,确保系统的稳定性和安全性。本文详细介绍IT系统工具开发的核心要素和最佳实践。更多学习教程www.fgedu.net.cn
$ uname -a
Linux fgedu-server 5.4.0-70-generic #78-Ubuntu SMP Fri Mar 19 13:29:52 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
# 检查开发环境
$ python3 –version
Python 3.8.10
$ go version
go version go1.16.3 linux/amd64
$ node –version
v14.17.0
# 检查系统资源
$ free -h
total used free shared buff/cache available
Mem: 16Gi 4.0Gi 10Gi 1.0Gi 2.0Gi 11Gi
Swap: 8.0Gi 0B 8.0Gi
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 100G 20G 80G 20% /
/dev/sdb1 500G 100G 400G 20% /data
# 检查系统服务
$ systemctl list-units –type=service –state=running | head -20
UNIT LOAD ACTIVE SUB DESCRIPTION
accounts-daemon.service loaded active running Accounts Service
apache2.service loaded active running The Apache HTTP Server
cron.service loaded active running Regular background program processing daemon
dbus.service loaded active running D-Bus System Message Bus
docker.service loaded active running Docker Application Container Engine
mysql.service loaded active running MySQL Community Server
nginx.service loaded active running A high performance web server and a reverse proxy server
ssh.service loaded active running OpenBSD Secure Shell server
systemd-journald.service loaded active running Journal Service
systemd-logind.service loaded active running Login Service
systemd-timesyncd.service loaded active running Network Time Synchronization
udev.service loaded active running udev Kernel Device Manager
2. 常用IT系统工具
常用的IT系统工具包括系统监控工具、自动化工具、备份工具、安全管理工具等。学习交流加群风哥微信: itpux-com
$ cat > it_system_tools.md << 'EOF' # 常用IT系统工具 ## 1. 系统监控工具 - Nagios:系统监控和告警 - Zabbix:企业级监控解决方案 - Prometheus:监控系统和服务 - Grafana:数据可视化和监控 - Datadog:云监控平台 ## 2. 自动化工具 - Ansible:自动化配置管理 - Puppet:自动化配置管理 - Chef:自动化配置管理 - SaltStack:自动化运维工具 - Terraform:基础设施即代码 ## 3. 备份工具 - Bacula:企业级备份解决方案 - Amanda:高级Maryland自动网络磁盘归档器 - Duplicity:加密备份工具 - Rsync:文件同步工具 - Tar:文件归档工具 ## 4. 安全管理工具 - Fail2ban:入侵防御软件 - OSSEC:主机入侵检测系统 - ClamAV:开源杀毒软件 - Lynis:安全审计工具 - OpenVAS:漏洞扫描器 ## 5. 日志管理工具 - ELK Stack:日志分析平台 - Graylog:日志管理平台 - Fluentd:数据收集器 - Logstash:日志处理管道 - Rsyslog:系统日志工具 EOF # 查看常用工具 $ cat it_system_tools.md # 常用IT系统工具 ## 1. 系统监控工具 - Nagios:系统监控和告警 - Zabbix:企业级监控解决方案 - Prometheus:监控系统和服务 - Grafana:数据可视化和监控 - Datadog:云监控平台 ## 2. 自动化工具 - Ansible:自动化配置管理 - Puppet:自动化配置管理 - Chef:自动化配置管理 - SaltStack:自动化运维工具 - Terraform:基础设施即代码 ## 3. 备份工具 - Bacula:企业级备份解决方案 - Amanda:高级Maryland自动网络磁盘归档器 - Duplicity:加密备份工具 - Rsync:文件同步工具 - Tar:文件归档工具 ## 4. 安全管理工具 - Fail2ban:入侵防御软件 - OSSEC:主机入侵检测系统 - ClamAV:开源杀毒软件 - Lynis:安全审计工具 - OpenVAS:漏洞扫描器 ## 5. 日志管理工具 - ELK Stack:日志分析平台 - Graylog:日志管理平台 - Fluentd:数据收集器 - Logstash:日志处理管道 - Rsyslog:系统日志工具
3. 工具设计原则
IT系统工具设计应遵循以下核心原则,确保工具的合理性和有效性。
$ cat > it_system_tool_design_principles.md << 'EOF' # IT系统工具设计原则 ## 1. 实用性原则 - 解决实际问题:针对IT系统管理中的实际需求 - 易用性:简单易用,减少学习成本 - 高效性:提高运维效率,减少人工操作 ## 2. 可靠性原则 - 稳定性:工具稳定运行,减少故障 - 容错性:能够处理异常情况 - 可恢复性:出现问题后能够恢复 ## 3. 可维护性原则 - 代码规范:统一代码风格 - 文档化:详细的文档 - 模块化:模块化设计,便于维护 ## 4. 安全性原则 - 权限控制:基于角色的访问控制 - 数据加密:保护敏感数据 - 审计日志:记录所有操作 ## 5. 可扩展性原则 - 插件机制:支持插件开发 - 配置化:通过配置调整功能 - API接口:提供API接口,便于集成 EOF # 查看设计原则 $ cat it_system_tool_design_principles.md # IT系统工具设计原则 ## 1. 实用性原则 - 解决实际问题:针对IT系统管理中的实际需求 - 易用性:简单易用,减少学习成本 - 高效性:提高运维效率,减少人工操作 ## 2. 可靠性原则 - 稳定性:工具稳定运行,减少故障 - 容错性:能够处理异常情况 - 可恢复性:出现问题后能够恢复 ## 3. 可维护性原则 - 代码规范:统一代码风格 - 文档化:详细的文档 - 模块化:模块化设计,便于维护 ## 4. 安全性原则 - 权限控制:基于角色的访问控制 - 数据加密:保护敏感数据 - 审计日志:记录所有操作 ## 5. 可扩展性原则 - 插件机制:支持插件开发 - 配置化:通过配置调整功能 - API接口:提供API接口,便于集成
4. 开发流程
IT系统工具开发的流程包括需求分析、设计、编码、测试、部署等环节。学习交流加群风哥QQ113257174
$ cat > it_system_development_process.md << 'EOF' # IT系统工具开发流程 ## 1. 需求分析 - 收集需求:与运维工程师沟通,了解实际需求 - 分析需求:分析需求的可行性和优先级 - 确定范围:明确工具的功能和边界 ## 2. 设计 - 架构设计:设计工具的架构和组件 - 技术选型:选择合适的技术栈 - 界面设计:设计工具的用户界面 - 数据库设计:设计数据存储结构 ## 3. 编码 - 搭建环境:搭建开发环境 - 编写代码:按照设计实现功能 - 代码审查:进行代码审查,确保代码质量 - 单元测试:编写单元测试,确保功能正确 ## 4. 测试 - 功能测试:测试工具的功能 - 性能测试:测试工具的性能 - 安全测试:测试工具的安全性 - 集成测试:测试工具与系统的集成 ## 5. 部署 - 打包:打包工具 - 部署:部署到生产环境 - 监控:监控工具的运行状态 - 维护:定期维护和更新 EOF # 查看开发流程 $ cat it_system_development_process.md # IT系统工具开发流程 ## 1. 需求分析 - 收集需求:与运维工程师沟通,了解实际需求 - 分析需求:分析需求的可行性和优先级 - 确定范围:明确工具的功能和边界 ## 2. 设计 - 架构设计:设计工具的架构和组件 - 技术选型:选择合适的技术栈 - 界面设计:设计工具的用户界面 - 数据库设计:设计数据存储结构 ## 3. 编码 - 搭建环境:搭建开发环境 - 编写代码:按照设计实现功能 - 代码审查:进行代码审查,确保代码质量 - 单元测试:编写单元测试,确保功能正确 ## 4. 测试 - 功能测试:测试工具的功能 - 性能测试:测试工具的性能 - 安全测试:测试工具的安全性 - 集成测试:测试工具与系统的集成 ## 5. 部署 - 打包:打包工具 - 部署:部署到生产环境 - 监控:监控工具的运行状态 - 维护:定期维护和更新
5. 系统监控工具开发
系统监控工具是IT系统工具的重要组成部分,负责监控系统的运行状态。更多学习教程公众号风哥教程itpux_com
$ mkdir -p system-monitoring-tool
# 初始化项目
$ cd system-monitoring-tool
$ npm init -y
$ npm install commander chalk
# 创建系统监控工具代码
$ cat > index.js << 'EOF'
#!/usr/bin/env node
const { program } = require('commander');
const chalk = require('chalk');
const os = require('os');
const fs = require('fs');
const { exec } = require('child_process');
program
.version('1.0.0')
.description('系统监控工具');
// CPU监控
program
.command('cpu')
.description('监控CPU使用率')
.option('-i, --interval
.action((options) => {
console.log(chalk.bold(‘\nCPU监控\n’));
const interval = parseInt(options.interval) * 1000;
setInterval(() => {
const cpus = os.cpus();
let totalIdle = 0;
let totalTick = 0;
cpus.forEach(cpu => {
for (let type in cpu.times) {
totalTick += cpu.times[type];
}
totalIdle += cpu.times.idle;
});
const usage = ((totalTick – totalIdle) / totalTick * 100).toFixed(2);
const date = new Date().toLocaleString();
console.log(chalk.blue(`[${date}] CPU使用率: ${usage}%`));
}, interval);
});
// 内存监控
program
.command(‘memory’)
.description(‘监控内存使用情况’)
.option(‘-i, –interval
.action((options) => {
console.log(chalk.bold(‘\n内存监控\n’));
const interval = parseInt(options.interval) * 1000;
setInterval(() => {
const total = os.totalmem();
const free = os.freemem();
const used = total – free;
const usage = (used / total * 100).toFixed(2);
const date = new Date().toLocaleString();
console.log(chalk.blue(`[${date}] 内存使用: ${(used / 1024 / 1024 / 1024).toFixed(2)}GB / ${(total / 1024 / 1024 / 1024).toFixed(2)}GB (${usage}%)`));
}, interval);
});
// 磁盘监控
program
.command(‘disk’)
.description(‘监控磁盘使用情况’)
.option(‘-p, –path
.action((options) => {
console.log(chalk.bold(‘\n磁盘监控\n’));
exec(`df -h ${options.path}`, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`监控失败: ${error.message}`));
return;
}
console.log(chalk.green(stdout));
});
});
// 网络监控
program
.command(‘network’)
.description(‘监控网络连接’)
.action(() => {
console.log(chalk.bold(‘\n网络监控\n’));
exec(‘netstat -an | grep ESTABLISHED | wc -l’, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`监控失败: ${error.message}`));
return;
}
console.log(chalk.green(`活跃连接数: ${stdout.trim()}`));
});
exec(‘ifconfig’, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`监控失败: ${error.message}`));
return;
}
console.log(chalk.green(‘\n网络接口信息:\n’));
console.log(stdout);
});
});
// 进程监控
program
.command(‘process’)
.description(‘监控进程状态’)
.option(‘-n, –name
.action((options) => {
console.log(chalk.bold(‘\n进程监控\n’));
let command = ‘ps aux –sort=-%mem | head -10’;
if (options.name) {
command = `ps aux | grep ${options.name}`;
}
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`监控失败: ${error.message}`));
return;
}
console.log(chalk.green(stdout));
});
});
// 系统概览
program
.command(‘overview’)
.description(‘系统概览’)
.action(() => {
console.log(chalk.bold(‘\n系统概览\n’));
console.log(chalk.green(‘系统信息:’));
console.log(`fgedu.net.cn: ${os.hostname()}`);
console.log(`操作系统: ${os.type()} ${os.release()}`);
console.log(`架构: ${os.arch()}`);
console.log(`CPU核心数: ${os.cpus().length}`);
console.log(`总内存: ${(os.totalmem() / 1024 / 1024 / 1024).toFixed(2)}GB`);
console.log(`运行时间: ${(os.uptime() / 3600).toFixed(2)}小时`);
console.log(chalk.green(‘\n负载情况:’));
const loadavg = os.loadavg();
console.log(`1分钟负载: ${loadavg[0].toFixed(2)}`);
console.log(`5分钟负载: ${loadavg[1].toFixed(2)}`);
console.log(`15分钟负载: ${loadavg[2].toFixed(2)}`);
});
// 执行命令
program.parse(process.argv);
EOF
# 测试系统监控工具
$ chmod +x index.js
$ ./index.js –version
1.0.0
# 系统概览
$ ./index.js overview
系统概览
系统信息:
fgedu.net.cn: fgedu-server
操作系统: Linux 5.4.0-70-generic
架构: x64
CPU核心数: 8
总内存: 16.00GB
运行时间: 100.00小时
负载情况:
1分钟负载: 0.50
5分钟负载: 0.60
15分钟负载: 0.70
# CPU监控(5秒后停止)
$ timeout 10 ./index.js cpu –interval 2
CPU监控
[2026-04-03 10:00:00] CPU使用率: 25.50%
[2026-04-03 10:00:02] CPU使用率: 30.20%
[2026-04-03 10:00:04] CPU使用率: 28.80%
[2026-04-03 10:00:06] CPU使用率: 32.10%
# 内存监控(5秒后停止)
$ timeout 10 ./index.js memory –interval 2
内存监控
[2026-04-03 10:00:00] 内存使用: 4.00GB / 16.00GB (25.00%)
[2026-04-03 10:00:02] 内存使用: 4.10GB / 16.00GB (25.63%)
[2026-04-03 10:00:04] 内存使用: 4.20GB / 16.00GB (26.25%)
[2026-04-03 10:00:06] 内存使用: 4.30GB / 16.00GB (26.88%)
# 磁盘监控
$ ./index.js disk –path /
磁盘监控
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 100G 20G 80G 20% /
# 网络监控
$ ./index.js network
网络监控
活跃连接数: 50
网络接口信息:
eth0: flags=4163
inet 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::215:5dff:fe00:1 prefixlen 64 scopeid 0x20
ether 00:15:5d:00:00:01 txqueuelen 1000 (Ethernet)
RX packets 1000000 bytes 1000000000 (953.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1000000 bytes 1000000000 (953.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# 进程监控
$ ./index.js process
进程监控
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1234 5.0 10.0 1000000 100000 ? Sl 10:00 0:30 /usr/bin/java -jar app.jar
mysql 5678 3.0 8.0 800000 80000 ? Sl 10:00 0:20 /usr/sbin/mysqld
www-data 9012 2.0 5.0 500000 50000 ? S 10:00 0:10 /usr/sbin/apache2 -k start
root 34567 1.0 3.0 300000 30000 ? Ss 10:00 0:05 /usr/bin/dockerd
root 45678 0.5 2.0 200000 20000 ? Ss 10:00 0:02 /usr/bin/containerd
6. 自动化工具开发
自动化工具是IT系统工具的重要组成部分,负责自动化执行系统管理任务。
$ mkdir -p automation-tool
# 初始化项目
$ cd automation-tool
$ npm init -y
$ npm install commander chalk
# 创建自动化工具代码
$ cat > index.js << 'EOF'
#!/usr/bin/env node
const { program } = require('commander');
const chalk = require('chalk');
const { exec } = require('child_process');
const fs = require('fs');
program
.version('1.0.0')
.description('自动化工具');
// 批量执行命令
program
.command('exec
.description(‘批量执行命令’)
.option(‘-h, –hosts
.option(‘-u, –user
.action((command, options) => {
console.log(chalk.bold(`\n批量执行命令: ${command}\n`));
const hosts = fs.readFileSync(options.hosts, ‘utf8’).split(‘\n’).filter(h => h.trim());
hosts.forEach(host => {
console.log(chalk.blue(`\n执行主机: ${host}`));
exec(`ssh ${options.user}@${host} “${command}”`, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`执行失败: ${error.message}`));
return;
}
console.log(chalk.green(stdout));
});
});
});
// 批量部署
program
.command(‘deploy
.description(‘批量部署软件包’)
.option(‘-h, –hosts
.option(‘-u, –user
.option(‘-d, –directory
.action((package, options) => {
console.log(chalk.bold(`\n批量部署: ${package}\n`));
const hosts = fs.readFileSync(options.hosts, ‘utf8’).split(‘\n’).filter(h => h.trim());
hosts.forEach(host => {
console.log(chalk.blue(`\n部署主机: ${host}`));
exec(`scp ${package} ${options.user}@${host}:${options.directory}/`, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`部署失败: ${error.message}`));
return;
}
console.log(chalk.green(`部署成功: ${host}`));
});
});
});
// 批量配置
program
.command(‘config
.description(‘批量配置系统’)
.option(‘-h, –hosts
.option(‘-u, –user
.action((file, options) => {
console.log(chalk.bold(`\n批量配置: ${file}\n`));
const hosts = fs.readFileSync(options.hosts, ‘utf8’).split(‘\n’).filter(h => h.trim());
const config = fs.readFileSync(file, ‘utf8’);
hosts.forEach(host => {
console.log(chalk.blue(`\n配置主机: ${host}`));
exec(`ssh ${options.user}@${host} “echo ‘${config}’ > /etc/sysctl.conf && sysctl -p”`, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`配置失败: ${error.message}`));
return;
}
console.log(chalk.green(`配置成功: ${host}`));
console.log(chalk.green(stdout));
});
});
});
// 批量服务管理
program
.command(‘service
.description(‘批量管理服务’)
.option(‘-h, –hosts
.option(‘-u, –user
.action((service, action, options) => {
console.log(chalk.bold(`\n批量管理服务: ${service} ${action}\n`));
const hosts = fs.readFileSync(options.hosts, ‘utf8’).split(‘\n’).filter(h => h.trim());
hosts.forEach(host => {
console.log(chalk.blue(`\n管理主机: ${host}`));
exec(`ssh ${options.user}@${host} “systemctl ${action} ${service}”`, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`管理失败: ${error.message}`));
return;
}
console.log(chalk.green(`管理成功: ${host}`));
console.log(chalk.green(stdout));
});
});
});
// 执行命令
program.parse(process.argv);
EOF
# 创建主机列表
$ cat > hosts.txt << 'EOF'
192.168.1.101
192.168.1.102
192.168.1.103
192.168.1.104
192.168.1.105
EOF
# 测试自动化工具
$ chmod +x index.js
$ ./index.js --version
1.0.0
# 批量执行命令
$ ./index.js exec "uptime" --hosts hosts.txt --user root
批量执行命令: uptime
执行主机: 192.168.1.101
10:00:00 up 100 days, 2:30, 1 user, load average: 0.50, 0.60, 0.70
执行主机: 192.168.1.102
10:00:00 up 100 days, 2:30, 1 user, load average: 0.40, 0.50, 0.60
执行主机: 192.168.1.103
10:00:00 up 100 days, 2:30, 1 user, load average: 0.30, 0.40, 0.50
执行主机: 192.168.1.104
10:00:00 up 100 days, 2:30, 1 user, load average: 0.20, 0.30, 0.40
执行主机: 192.168.1.105
10:00:00 up 100 days, 2:30, 1 user, load average: 0.10, 0.20, 0.30
# 批量服务管理
$ ./index.js service nginx restart --hosts hosts.txt --user root
批量管理服务: nginx restart
管理主机: 192.168.1.101
管理成功: 192.168.1.101
管理主机: 192.168.1.102
管理成功: 192.168.1.102
管理主机: 192.168.1.103
管理成功: 192.168.1.103
管理主机: 192.168.1.104
管理成功: 192.168.1.104
管理主机: 192.168.1.105
管理成功: 192.168.1.105
7. 备份工具开发
备份工具是IT系统工具的重要组成部分,负责系统数据的备份和恢复。
$ mkdir -p backup-tool
# 初始化项目
$ cd backup-tool
$ npm init -y
$ npm install commander chalk
# 创建备份工具代码
$ cat > index.js << 'EOF'
#!/usr/bin/env node
const { program } = require('commander');
const chalk = require('chalk');
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
program
.version('1.0.0')
.description('备份工具');
// 文件备份
program
.command('file
.description(‘备份文件或目录’)
.option(‘-d, –destination
.option(‘-c, –compress’, ‘压缩备份’, false)
.action((source, options) => {
console.log(chalk.bold(`\n备份文件: ${source}\n`));
const timestamp = new Date().toISOString().replace(/[:.]/g, ‘-‘);
const basename = path.basename(source);
let backupFile = `${options.destination}/${basename}_${timestamp}`;
if (options.compress) {
backupFile += ‘.tar.gz’;
exec(`tar -czf ${backupFile} ${source}`, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`备份失败: ${error.message}`));
return;
}
console.log(chalk.green(`备份成功: ${backupFile}`));
exec(`ls -lh ${backupFile}`, (error, stdout, stderr) => {
if (!error) {
console.log(chalk.blue(stdout));
}
});
});
} else {
exec(`cp -r ${source} ${backupFile}`, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`备份失败: ${error.message}`));
return;
}
console.log(chalk.green(`备份成功: ${backupFile}`));
});
}
});
# 数据库备份
program
.command(‘database
.description(‘备份数据库’)
.option(‘-h, –host
.option(‘-u, –user
.option(‘-p, –password
.option(‘-d, –destination
.action((database, options) => {
console.log(chalk.bold(`\n备份数据库: ${database}\n`));
const timestamp = new Date().toISOString().replace(/[:.]/g, ‘-‘);
const backupFile = `${options.destination}/${database}_${timestamp}.sql`;
exec(`mysqldump -h ${options.host} -u ${options.user} -p${options.password} ${database} > ${backupFile}`, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`备份失败: ${error.message}`));
return;
}
console.log(chalk.green(`备份成功: ${backupFile}`));
exec(`ls -lh ${backupFile}`, (error, stdout, stderr) => {
if (!error) {
console.log(chalk.blue(stdout));
}
});
});
});
# 增量备份
program
.command(‘incremental
.description(‘增量备份’)
.option(‘-d, –destination
.option(‘-l, –link-dest
.action((source, options) => {
console.log(chalk.bold(`\n增量备份: ${source}\n`));
const timestamp = new Date().toISOString().replace(/[:.]/g, ‘-‘);
const backupDir = `${options.destination}/${path.basename(source)}_${timestamp}`;
let command = `rsync -av –delete ${source}/ ${backupDir}/`;
if (options.linkDest) {
command = `rsync -av –delete –link-dest=${options.linkDest} ${source}/ ${backupDir}/`;
}
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`备份失败: ${error.message}`));
return;
}
console.log(chalk.green(`备份成功: ${backupDir}`));
console.log(chalk.blue(stdout));
});
});
# 备份清理
program
.command(‘clean
.description(‘清理旧备份’)
.option(‘-d, –days
.action((directory, options) => {
console.log(chalk.bold(`\n清理旧备份: ${directory}\n`));
const days = parseInt(options.days);
exec(`find ${directory} -type f -mtime +${days} -delete`, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`清理失败: ${error.message}`));
return;
}
console.log(chalk.green(`已清理 ${days} 天前的备份`));
});
});
# 备份列表
program
.command(‘list
.description(‘列出备份文件’)
.action((directory) => {
console.log(chalk.bold(`\n备份列表: ${directory}\n`));
exec(`ls -lh ${directory}`, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`列出失败: ${error.message}`));
return;
}
console.log(chalk.green(stdout));
});
});
# 执行命令
program.parse(process.argv);
EOF
# 测试备份工具
$ chmod +x index.js
$ ./index.js –version
1.0.0
# 文件备份
$ ./index.js file /etc/nginx –destination /backup –compress
备份文件: /etc/nginx
备份成功: /backup/nginx_2026-04-03T10-00-00-000Z.tar.gz
-rw-r–r– 1 root root 1.0M Apr 3 10:00 /backup/nginx_2026-04-03T10-00-00-000Z.tar.gz
# 数据库备份
$ ./index.js database fgedu_hr –host fgedudb –user root –password password –destination /backup
备份数据库: fgedu_hr
备份成功: /backup/fgedu_hr_2026-04-03T10-00-00-000Z.sql
-rw-r–r– 1 root root 10M Apr 3 10:00 /backup/fgedu_hr_2026-04-03T10-00-00-000Z.sql
# 备份列表
$ ./index.js list /backup
备份列表: /backup
total 20M
-rw-r–r– 1 root root 1.0M Apr 3 10:00 nginx_2026-04-03T10-00-00-000Z.tar.gz
-rw-r–r– 1 root root 10M Apr 3 10:00 fgedu_hr_2026-04-03T10-00-00-000Z.sql
# 备份清理
$ ./index.js clean /backup –days 7
清理旧备份: /backup
已清理 7 天前的备份
8. 安全管理工具开发
安全管理工具是IT系统工具的重要组成部分,负责系统的安全管理和审计。
$ mkdir -p security-tool
# 初始化项目
$ cd security-tool
$ npm init -y
$ npm install commander chalk
# 创建安全管理工具代码
$ cat > index.js << 'EOF'
#!/usr/bin/env node
const { program } = require('commander');
const chalk = require('chalk');
const { exec } = require('child_process');
const fs = require('fs');
program
.version('1.0.0')
.description('安全管理工具');
# 安全扫描
program
.command('scan')
.description('安全扫描')
.option('-t, --type
.action((options) => {
console.log(chalk.bold(‘\n安全扫描\n’));
if (options.type === ‘basic’) {
console.log(chalk.blue(‘执行基础安全扫描…\n’));
exec(‘cat /etc/passwd | grep -v nologin | grep -v false’, (error, stdout, stderr) => {
console.log(chalk.green(‘可登录用户:’));
console.log(stdout);
});
exec(‘netstat -tuln’, (error, stdout, stderr) => {
console.log(chalk.green(‘开放端口:’));
console.log(stdout);
});
exec(‘ps aux –sort=-%mem | head -10’, (error, stdout, stderr) => {
console.log(chalk.green(‘资源占用TOP 10:’));
console.log(stdout);
});
}
});
# 用户审计
program
.command(‘audit-users’)
.description(‘用户审计’)
.action(() => {
console.log(chalk.bold(‘\n用户审计\n’));
exec(‘cat /etc/passwd’, (error, stdout, stderr) => {
const users = stdout.split(‘\n’).filter(u => u.trim());
console.log(chalk.green(‘用户列表:’));
users.forEach(user => {
const parts = user.split(‘:’);
const username = parts[0];
const uid = parts[2];
const shell = parts[6];
if (uid === ‘0’) {
console.log(chalk.red(`[ROOT] ${username} (UID: ${uid}, Shell: ${shell})`));
} else if (shell && !shell.includes(‘nologin’) && !shell.includes(‘false’)) {
console.log(chalk.yellow(`[LOGIN] ${username} (UID: ${uid}, Shell: ${shell})`));
}
});
});
});
# 端口审计
program
.command(‘audit-ports’)
.description(‘端口审计’)
.action(() => {
console.log(chalk.bold(‘\n端口审计\n’));
exec(‘netstat -tuln’, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`审计失败: ${error.message}`));
return;
}
const lines = stdout.split(‘\n’).filter(l => l.trim());
console.log(chalk.green(‘开放端口:’));
lines.forEach(line => {
if (line.includes(‘LISTEN’)) {
const parts = line.split(/\s+/);
const proto = parts[0];
const port = parts[3].split(‘:’).pop();
console.log(chalk.blue(`${proto}: ${port}`));
}
});
});
});
# 日志审计
program
.command(‘audit-logs’)
.description(‘日志审计’)
.option(‘-f, –file
.option(‘-n, –number
.action((options) => {
console.log(chalk.bold(‘\n日志审计\n’));
exec(`tail -n ${options.number} ${options.file}`, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`审计失败: ${error.message}`));
return;
}
const lines = stdout.split(‘\n’);
lines.forEach(line => {
if (line.includes(‘Failed’) || line.includes(‘Invalid’)) {
console.log(chalk.red(line));
} else if (line.includes(‘Accepted’)) {
console.log(chalk.green(line));
} else if (line.trim()) {
console.log(chalk.blue(line));
}
});
});
});
# 防火墙管理
program
.command(‘firewall
.description(‘防火墙管理’)
.option(‘-p, –port
.option(‘-s, –service
.action((action, options) => {
console.log(chalk.bold(`\n防火墙管理: ${action}\n`));
let command = ”;
switch (action) {
case ‘status’:
command = ‘ufw status’;
break;
case ‘enable’:
command = ‘ufw enable’;
break;
case ‘disable’:
command = ‘ufw disable’;
break;
case ‘allow’:
if (options.port) {
command = `ufw allow ${options.port}`;
} else if (options.service) {
command = `ufw allow ${options.service}`;
}
break;
case ‘deny’:
if (options.port) {
command = `ufw deny ${options.port}`;
} else if (options.service) {
command = `ufw deny ${options.service}`;
}
break;
}
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(chalk.red(`操作失败: ${error.message}`));
return;
}
console.log(chalk.green(stdout));
});
});
# 执行命令
program.parse(process.argv);
EOF
# 测试安全管理工具
$ chmod +x index.js
$ ./index.js –version
1.0.0
# 安全扫描
$ ./index.js scan –type basic
安全扫描
执行基础安全扫描…
可登录用户:
root:x:0:0:root:/root:/bin/bash
fengge:x:1000:1000:www.itpux.com:/home/fengge:/bin/bash
开放端口:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN
资源占用TOP 10:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1234 5.0 10.0 1000000 100000 ? Sl 10:00 0:30 /usr/bin/java -jar app.jar
mysql 5678 3.0 8.0 800000 80000 ? Sl 10:00 0:20 /usr/sbin/mysqld
# 用户审计
$ ./index.js audit-users
用户审计
用户列表:
[ROOT] root (UID: 0, Shell: /bin/bash)
[LOGIN] fengge (UID: 1000, Shell: /bin/bash)
# 端口审计
$ ./index.js audit-ports
端口审计
开放端口:
tcp: 22
tcp: 80
tcp: 443
# 防火墙管理
$ ./index.js firewall status
防火墙管理: status
Status: active
To Action From
— —— —-
22/tcp ALLOW Anywhere
80/tcp ALLOW Anywhere
443/tcp ALLOW Anywhere
9. 工具实现
工具实现是将设计转化为实际工具的过程,包括编码、测试、部署等环节。author:www.itpux.com
$ mkdir -p it-system-toolkit
# 初始化项目
$ cd it-system-toolkit
$ npm init -y
$ npm install commander chalk
# 创建工具代码
$ cat > index.js << 'EOF'
#!/usr/bin/env node
const { program } = require('commander');
const chalk = require('chalk');
const os = require('os');
const { exec } = require('child_process');
program
.version('1.0.0')
.description('IT系统工具包');
# 系统信息
program
.command('info')
.description('系统信息')
.action(() => {
console.log(chalk.bold(‘\n系统信息\n’));
console.log(`fgedu.net.cn: ${os.hostname()}`);
console.log(`操作系统: ${os.type()} ${os.release()}`);
console.log(`架构: ${os.arch()}`);
console.log(`CPU核心数: ${os.cpus().length}`);
console.log(`总内存: ${(os.totalmem() / 1024 / 1024 / 1024).toFixed(2)}GB`);
console.log(`运行时间: ${(os.uptime() / 3600).toFixed(2)}小时`);
});
# CPU监控
program
.command(‘cpu’)
.description(‘CPU监控’)
.action(() => {
const cpus = os.cpus();
let totalIdle = 0;
let totalTick = 0;
cpus.forEach(cpu => {
for (let type in cpu.times) {
totalTick += cpu.times[type];
}
totalIdle += cpu.times.idle;
});
const usage = ((totalTick – totalIdle) / totalTick * 100).toFixed(2);
console.log(chalk.bold(‘\nCPU监控\n’));
console.log(`CPU使用率: ${usage}%`);
console.log(`CPU核心数: ${cpus.length}`);
});
# 内存监控
program
.command(‘memory’)
.description(‘内存监控’)
.action(() => {
const total = os.totalmem();
const free = os.freemem();
const used = total – free;
const usage = (used / total * 100).toFixed(2);
console.log(chalk.bold(‘\n内存监控\n’));
console.log(`总内存: ${(total / 1024 / 1024 / 1024).toFixed(2)}GB`);
console.log(`已使用: ${(used / 1024 / 1024 / 1024).toFixed(2)}GB`);
console.log(`可用: ${(free / 1024 / 1024 / 1024).toFixed(2)}GB`);
console.log(`使用率: ${usage}%`);
});
# 磁盘监控
program
.command(‘disk’)
.description(‘磁盘监控’)
.action(() => {
exec(‘df -h’, (error, stdout, stderr) => {
console.log(chalk.bold(‘\n磁盘监控\n’));
console.log(stdout);
});
});
# 网络监控
program
.command(‘network’)
.description(‘网络监控’)
.action(() => {
exec(‘ifconfig’, (error, stdout, stderr) => {
console.log(chalk.bold(‘\n网络监控\n’));
console.log(stdout);
});
});
# 执行命令
program.parse(process.argv);
EOF
# 测试工具
$ chmod +x index.js
$ ./index.js –version
1.0.0
# 系统信息
$ ./index.js info
系统信息
fgedu.net.cn: fgedu-server
操作系统: Linux 5.4.0-70-generic
架构: x64
CPU核心数: 8
总内存: 16.00GB
运行时间: 100.00小时
# CPU监控
$ ./index.js cpu
CPU监控
CPU使用率: 25.50%
CPU核心数: 8
# 内存监控
$ ./index.js memory
内存监控
总内存: 16.00GB
已使用: 4.00GB
可用: 12.00GB
使用率: 25.00%
# 磁盘监控
$ ./index.js disk
磁盘监控
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 100G 20G 80G 20% /
/dev/sdb1 500G 100G 400G 20% /data
# 网络监控
$ ./index.js network
网络监控
eth0: flags=4163
inet 192.168.1.100 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::215:5dff:fe00:1 prefixlen 64 scopeid 0x20
ether 00:15:5d:00:00:01 txqueuelen 1000 (Ethernet)
RX packets 1000000 bytes 1000000000 (953.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1000000 bytes 1000000000 (953.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
10. 最佳实践
IT系统工具开发的最佳实践包括规划、实施、测试等多个方面,以下是一些关键建议。
- 采用模块化设计,提高工具的可维护性和可扩展性
- 实施版本控制,便于代码管理和回滚
- 建立完善的测试体系,确保工具的可靠性
- 定期更新工具,适应新的系统环境和技术
- 制定详细的使用文档,方便其他运维工程师使用
- 持续优化工具性能,提高运行效率
- 对工具进行安全审计,确保安全性
$ time ./index.js info
系统信息
fgedu.net.cn: fgedu-server
操作系统: Linux 5.4.0-70-generic
架构: x64
CPU核心数: 8
总内存: 16.00GB
运行时间: 100.00小时
real 0m0.100s
user 0m0.050s
sys 0m0.050s
# 工具可靠性测试
$ for i in {1..10}; do ./index.js cpu; done
CPU监控
CPU使用率: 25.50%
CPU核心数: 8
# 工具安装和部署
$ npm pack
npm notice
npm notice package: it-system-toolkit@1.0.0
npm notice === Tarball Contents ===
npm notice 1.0kB package.json
npm notice 3.0kB index.js
npm notice === Tarball Details ===
npm notice name: it-system-toolkit
npm notice version: 1.0.0
npm notice filename: it-system-toolkit-1.0.0.tgz
npm notice package size: 1.5 kB
npm notice unpacked size: 4.0 kB
npm notice shasum: abc123def456
npm notice integrity: sha512-xyz789
npm notice total files: 2
npm notice
$ npm install -g it-system-toolkit-1.0.0.tgz
$ it-system-toolkit –version
1.0.0
通过以上步骤,我们成功设计并实现了一个完整的IT系统工具包,包括系统监控、自动化、备份、安全管理等多个方面。在实际操作中,应根据具体的系统管理需求和技术环境进行调整,确保工具的合理性和有效性。
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
