Learn Golang for DevOps with hands-on tasks and real-world automation examples. Ideal for DevOps engineers looking to build practical tools and level up their skills. Connects to Kubernetes and other DevOps workflows.
git clone https://github.com/techiescamp/golang-for-devops.gitLearn Golang for DevOps with hands-on tasks and real-world automation examples. Ideal for DevOps engineers looking to build practical tools and level up their skills. Connects to Kubernetes and other DevOps workflows.
1. **Install Dependencies**: Run `go mod init disk-monitor` followed by `go get github.com/shirou/gopsutil/v3/disk` to install the required package. 2. **Customize the Program**: Replace the disk monitoring logic with your specific requirements (e.g., check `/var/log` instead of all disks). Modify the JSON output structure if needed. 3. **Add to Systemd**: Create a systemd service file at `/etc/systemd/system/disk-monitor.service` with: ```ini [Unit] Description=Disk Usage Monitor After=network.target [Service] ExecStart=/usr/local/bin/disk-monitor Restart=always User=root [Install] WantedBy=multi-user.target ``` Then run: ```bash sudo systemctl daemon-reload sudo systemctl enable disk-monitor sudo systemctl start disk-monitor ``` 4. **Test and Debug**: Run the program manually with `go run main.go` to verify it works. Check logs with `journalctl -u disk-monitor -f`. 5. **Integrate with Alerting**: Use the JSON output to trigger alerts (e.g., Prometheus alerts, Slack notifications). For example, parse the JSON in a monitoring tool like Grafana or use it as input for a custom alerting script.
No install command available. Check the GitHub repository for manual installation instructions.
git clone https://github.com/techiescamp/golang-for-devopsCopy the install command above and run it in your terminal.
Launch Claude Code, Cursor, or your preferred AI coding agent.
Use the prompt template or examples below to test the skill.
Adapt the skill to your specific use case and workflow.
Write a Go program that [ACTION] in a DevOps environment. Use the [LIBRARY/TOOL] package to interact with [SYSTEM/COMPONENT]. Include error handling for [COMMON_ERROR_CASES]. The program should output [EXPECTED_OUTPUT_FORMAT] and be optimized for [PERFORMANCE/SECURITY/OTHER_CRITERIA]. Example: 'Write a Go program that monitors disk usage on a Linux server using the github.com/shirou/gopsutil package. Handle cases where the disk is 90% full or higher. The program should output a JSON report with timestamp, disk name, and usage percentage, and run as a background service with automatic restarts.'
```go
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"time"
"github.com/shirou/gopsutil/v3/disk"
)
// DiskUsageReport represents the structure of the JSON output
type DiskUsageReport struct {
Timestamp string `json:"timestamp"`
DiskName string `json:"disk_name"`
UsagePercent float64 `json:"usage_percent"`
Status string `json:"status"`
}
func main() {
// Get disk partitions
partitions, err := disk.Partitions(false)
if err != nil {
log.Fatalf("Failed to get disk partitions: %v", err)
}
// Filter for physical disks (exclude tmpfs, etc.)
var physicalDisks []string
for _, p := range partitions {
if p.Fstype != "tmpfs" && p.Fstype != "devtmpfs" {
physicalDisks = append(physicalDisks, p.Mountpoint)
}
}
// Check usage for each disk
reports := make([]DiskUsageReport, 0)
for _, mountpoint := range physicalDisks {
usage, err := disk.Usage(mountpoint)
if err != nil {
log.Printf("Failed to get usage for %s: %v", mountpoint, err)
continue
}
status := "healthy"
if usage.UsedPercent >= 90 {
status = "critical"
} else if usage.UsedPercent >= 80 {
status = "warning"
}
report := DiskUsageReport{
Timestamp: time.Now().Format(time.RFC3339),
DiskName: mountpoint,
UsagePercent: usage.UsedPercent,
Status: status,
}
reports = append(reports, report)
// Print to console for immediate visibility
fmt.Printf("Disk %s: %.2f%% used (%s)\n", mountpoint, usage.UsedPercent, status)
}
// Write JSON report to file
jsonData, err := json.MarshalIndent(reports, "", " ")
if err != nil {
log.Fatalf("Failed to marshal JSON: %v", err)
}
err = os.WriteFile("/var/log/disk_usage_report.json", jsonData, 0644)
if err != nil {
log.Fatalf("Failed to write JSON report: %v", err)
}
// Exit with non-zero code if any disk is in critical state
for _, r := range reports {
if r.Status == "critical" {
os.Exit(1)
}
}
}
```
### Example Output (JSON Report):
```json
[
{
"timestamp": "2023-11-15T14:30:45Z",
"disk_name": "/",
"usage_percent": 78.5,
"status": "warning"
},
{
"timestamp": "2023-11-15T14:30:45Z",
"disk_name": "/var",
"usage_percent": 92.3,
"status": "critical"
},
{
"timestamp": "2023-11-15T14:30:45Z",
"disk_name": "/home",
"usage_percent": 45.2,
"status": "healthy"
}
]
```
Console Output:
```
Disk /: 78.50% used (warning)
Disk /var: 92.30% used (critical)
Disk /home: 45.20% used (healthy)
```Your one-stop shop for church and ministry supplies.
Automate your browser workflows effortlessly
Cloud ETL platform for non-technical data integration
IronCalc is a spreadsheet engine and ecosystem
Orchestrate workloads with multi-cloud support, job scheduling, and integrated service discovery features.
Monitor frontend performance and debug effectively with session replay and analytics.
Take a free 3-minute scan and get personalized AI skill recommendations.
Take free scan