{"skill":{"slug":"cmdnotify","displayName":"Cmdnotify","summary":"Lightweight command monitoring tool that periodically executes local commands/scripts in batches, detects output changes, and triggers alerts. Built with Go...","description":"---\nname: cmdnotify\ndescription: Lightweight command monitoring tool that periodically executes local commands/scripts in batches, detects output changes, and triggers alerts. Built with Go for minimal resource consumption.\ntags: [monitoring, alerting, devops, system, go, cli]\nauthor: qhz.hetu\nversion: 1.0.0\n---\n\n# CmdNotify - Command Monitor Skill\n\n## 触发词\n\n当用户提到以下关键词时激活此技能：\n- \"监控命令\"\n- \"命令告警\"\n- \"定时执行脚本\"\n- \"输出变化检测\"\n- \"cmdnotify\"\n- \"command monitor\"\n- \"batch script execution\"\n\n## 使用场景\n\n- 批量监控多个系统命令的输出变化\n- 定时执行脚本并检测异常\n- 磁盘使用率、内存、进程数等系统指标监控\n- API 健康检查与状态变化告警\n- 配置文件漂移检测\n- 日志模式监控\n\n## Overview\n\nCmdNotify is a lightweight, resource-optimized command monitoring system written in Go. It allows you to:\n\n- **Batch monitor** multiple commands/scripts simultaneously\n- Set **custom execution intervals** for each command\n- Automatically **detect output changes** (stdout/stderr + exit code)\n- **Trigger alerts** when changes are detected\n- Run with **minimal CPU/memory footprint** using goroutine pools and timeouts\n\n## Quick Start\n\n### 1. Configuration\n\nCreate a `config.json` file:\n\n```json\n{\n  \"commands\": [\n    {\n      \"name\": \"disk_usage\",\n      \"command\": \"df -h /\",\n      \"interval\": \"30s\",\n      \"timeout\": \"10s\",\n      \"notify_on\": [\"change\"],\n      \"max_history\": 2\n    },\n    {\n      \"name\": \"memory_check\",\n      \"command\": \"vm_stat | grep 'Pages free'\",\n      \"interval\": \"20s\",\n      \"timeout\": \"5s\",\n      \"notify_on\": [\"change\"],\n      \"notify_cmd\": \"echo '[ALERT] $CMD_NAME: $CMD_MESSAGE'\",\n      \"max_history\": 2\n    }\n  ]\n}\n```\n\n### 2. Build & Run\n\n```bash\n# Build\ngo build -o cmdnotify .\n\n# Run with default config\n./cmdnotify\n\n# Run with custom config\n./cmdnotify -config /path/to/config.json\n```\n\n## Configuration Options\n\n| Field | Type | Default | Description |\n|-------|------|---------|-------------|\n| `name` | string | required | Unique identifier for the command |\n| `command` | string | required | Shell command to execute |\n| `interval` | duration | `1s` | Execution interval (e.g., `30s`, `5m`, `1h`) |\n| `timeout` | duration | `30s` | Maximum execution time per run |\n| `notify_on` | []string | `[]` | Events to notify on: `[\"change\"]`, `[\"error\"]`, `[\"all\"]` |\n| `notify_cmd` | string | `\"\"` | Custom notification command (optional) |\n| `max_history` | int | `2` | Number of results to keep for change detection |\n| `working_dir` | string | `\"\"` | Working directory for command execution |\n\n## Notification\n\n### Default Behavior\nWhen no `notify_cmd` is specified, alerts are printed to `stderr`:\n```\n[disk_usage] Command 'disk_usage' output changed\nExit code: 0\nOutput:\nFilesystem      Size   Used  Avail Capacity\n/dev/disk1s1   228Gi  180Gi   48Gi    79%\n```\n\n### Custom Notification\nSet `notify_cmd` to integrate with external systems:\n\n```json\n{\n  \"notify_cmd\": \"curl -X POST -d '{\\\"text\\\":\\\"$CMD_MESSAGE\\\"}' https://hooks.slack.com/services/...\"\n}\n```\n\nEnvironment variables available in `notify_cmd`:\n- `CMD_NAME` - Command name\n- `CMD_MESSAGE` - Alert message\n\n## Resource Optimization\n\nCmdNotify is designed for minimal resource consumption:\n\n| Feature | Implementation |\n|---------|---------------|\n| **Goroutine Pool** | Semaphore-based concurrency limiting (default: CPU count, min 4) |\n| **Timeout Control** | Per-command `context.WithTimeout` prevents zombie processes |\n| **Memory Reuse** | `bytes.Buffer` for output; history limited to `max_history` |\n| **Efficient Hashing** | SHA-256 of output + exit code for fast change detection |\n| **Graceful Shutdown** | `context.Cancel` + `sync.WaitGroup` ensures clean exit |\n\n## Project Structure\n\n```\nCmdNotify/\n├── config.go      # Configuration loading & validation\n├── config.json    # Example configuration\n├── executor.go    # Command execution & change detection\n├── scheduler.go   # Scheduling engine with goroutine pool\n├── main.go        # Entry point\n└── go.mod         # Go module definition\n```\n\n## Use Cases\n\n- Monitor disk usage changes\n- Track process counts\n- Watch log file patterns\n- Check service health endpoints\n- Detect configuration drift\n- Monitor system resource usage\n\n## Advanced Example\n\n```json\n{\n  \"commands\": [\n    {\n      \"name\": \"api_health\",\n      \"command\": \"curl -s -o /dev/null -w '%{http_code}' http://localhost:8080/health\",\n      \"interval\": \"10s\",\n      \"timeout\": \"5s\",\n      \"notify_on\": [\"change\", \"error\"],\n      \"notify_cmd\": \"echo '[$(date)] $CMD_NAME status changed: $CMD_MESSAGE' >> /var/log/alerts.log\",\n      \"max_history\": 3\n    },\n    {\n      \"name\": \"db_connection_count\",\n      \"command\": \"psql -c 'SELECT count(*) FROM pg_stat_activity;' -t\",\n      \"interval\": \"1m\",\n      \"timeout\": \"10s\",\n      \"notify_on\": [\"all\"],\n      \"working_dir\": \"/opt/monitoring\"\n    }\n  ]\n}\n```\n","topics":["Go","Monitoring","Alerting","CLI","Devops"],"tags":{"latest":"1.0.1"},"stats":{"comments":0,"downloads":359,"installsAllTime":13,"installsCurrent":0,"stars":0,"versions":2},"createdAt":1778330181310,"updatedAt":1778492888959},"latestVersion":{"version":"1.0.1","createdAt":1778330659344,"changelog":"- Updated author field in SKILL.md to \"qhz.hetu\"  \n- No feature or functionality changes; documentation only  \n- Version number in SKILL.md remains at 1.0.0","license":"MIT-0"},"metadata":{"setup":[],"os":null,"systems":null},"owner":{"handle":"mengyun8","userId":"s1735y9pmsvqz4qh9wk1m67z3d86c3bp","displayName":"mengyun","image":"https://avatars.githubusercontent.com/u/10460982?v=4"},"moderation":null}