Skip to content

Commit f68d679

Browse files
committed
feat(update): internal handlers
1 parent c85cdf1 commit f68d679

File tree

3 files changed

+76
-64
lines changed

3 files changed

+76
-64
lines changed

README.md

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,66 @@
22

33
[![Visit Nodebyte](https://nodebyte.host/banner.png)](https://nodebyte.host)
44

5-
**SysCapture** is an open source hardware monitoring agent that collects vital system information and exposes it via a RESTful API.
6-
**Note:** SysCapture is currently available only on **Linux**.
5+
**SysCapture** is an open source hardware monitoring agent that collects vital system information and exposes it via a RESTful API for easy integration with monitoring services like Prometheus.
6+
7+
> **Note:** SysCapture is currently available only on **Linux**.
78
89
---
910

1011
## Features
1112

12-
- **Hardware Monitoring:** Collects CPU, memory, disk, and host details.
13-
- **RESTful API:** Easily retrieve metrics using HTTP endpoints.
14-
- **Lightweight:** Designed to run with minimal system overhead.
15-
- **Open Source:** Extend and customize to suit your needs.
13+
- **Hardware Monitoring:** Captures CPU, memory, disk, and host details.
14+
- **RESTful API:** Retrieve metrics quickly via HTTP endpoints.
15+
- **Lightweight:** Minimal system overhead.
16+
- **Extensible:** Fully open source, allowing for customization.
1617

1718
---
1819

19-
## System Installation
20+
## Installation
2021

21-
### Pre-built Binaries
22+
### Using Pre-built Binaries (Recommended)
2223

23-
We highly recommend using our pre-built binaries to get started quickly. Simply download the appropriate binary for your platform from the [GitHub Releases](https://github.com/nodebytehosting/syscapture/releases) page.
24+
Download the latest release for your platform from the [GitHub Releases](https://github.com/nodebytehosting/syscapture/releases) page. This is the easiest way to get started.
2425

2526
### Go Package Installation
2627

27-
You can also install SysCapture using Go:
28+
Alternatively, you can install SysCapture via Go:
2829

2930
```shell
3031
go install github.com/nodebytehosting/syscapture/cmd/syscapture@latest
3132
```
3233

3334
---
3435

35-
## Environment Variables
36+
## Configuration
3637

37-
Configure SysCapture using the following environment variables:
38+
SysCapture requires a few environment variables to run correctly. Configure these in your shell or within your service manager:
3839

3940
| Variable | Description | Required/Optional |
4041
| ------------ | -------------------------------------------- | ----------------- |
4142
| `PORT` | Port on which SysCapture listens | Optional |
4243
| `API_SECRET` | Secret key used for API authentication | **Required** |
4344
| `GIN_MODE` | Operating mode for the Gin framework | Optional |
4445

45-
> **Important:** Replace `your_secret` with a strong, secure secret. This secret must also be configured in Checkmate’s infrastructure monitoring dashboard for proper integration.
46+
> **Important:** Replace `your_secret` with a strong, secure secret. This key must match the configuration in Checkmate’s infrastructure monitoring dashboard.
4647
4748
### Example Configuration
4849

49-
Set the environment variables in your shell (adjust values as needed):
50+
Set the environment variables as needed:
5051

5152
```shell
5253
export API_SECRET=your_secret
5354
export GIN_MODE=release
54-
export PORT=your_port # Optional: defaults to 59232 if not set
55+
export PORT=your_port # Defaults to 59232 if not set
5556
```
5657

57-
To run SysCapture with minimal configuration:
58+
To run SysCapture:
5859

5960
```shell
6061
API_SECRET=your_secret ./syscapture
6162
```
6263

63-
Or with a full configuration:
64+
Or, for a full configuration:
6465

6566
```shell
6667
API_SECRET=your_secret GIN_MODE=release PORT=your_port ./syscapture
@@ -70,21 +71,21 @@ API_SECRET=your_secret GIN_MODE=release PORT=your_port ./syscapture
7071

7172
## Running as a systemd Service
7273

73-
For a more robust local setup, you can run SysCapture as a systemd service to ensure it starts on boot and automatically restarts if it fails.
74+
For a robust and continuously running setup, deploy SysCapture as a systemd service.
7475

75-
#### Sample systemd Service File
76+
### Sample systemd Service File
7677

77-
Create a file at `/etc/systemd/system/syscapture.service` with the following content (be sure to update paths and usernames as needed):
78+
Create a file at `/etc/systemd/system/syscapture.service` with the following content (update paths and usernames as needed):
7879

7980
```ini
8081
[Unit]
8182
Description=SysCapture Monitoring Agent
8283
After=network.target
8384

8485
[Service]
85-
User=your_username # Change this to your local username
86-
Group=your_username # Change this accordingly
87-
ExecStart=/path/to/syscapture # Replace with the full path to your binary
86+
User=your_username # Replace with your local username
87+
Group=your_username # Replace with your local group
88+
ExecStart=/full/path/to/syscapture # Full path to the SysCapture binary
8889
Restart=always
8990
Environment="API_SECRET=your_secret"
9091
Environment="GIN_MODE=release"
@@ -94,56 +95,51 @@ Environment="PORT=59232"
9495
WantedBy=multi-user.target
9596
```
9697

97-
#### Setup Steps
98+
### Setup Steps
9899

99-
1. **Copy the service file:**
100-
Save the above configuration to `/etc/systemd/system/syscapture.service`.
100+
1. **Copy the Service File:**
101+
Save the above content to `/etc/systemd/system/syscapture.service`.
101102

102-
2. **Reload systemd configuration:**
103+
2. **Reload systemd:**
103104

104105
```shell
105106
sudo systemctl daemon-reload
106107
```
107108

108-
3. **Enable the service:**
109+
3. **Enable and Start the Service:**
109110

110111
```shell
111112
sudo systemctl enable syscapture
112-
```
113-
114-
4. **Start the service:**
115-
116-
```shell
117113
sudo systemctl start syscapture
118114
```
119115

120-
5. **Check the service status:**
116+
4. **Check Service Status:**
121117

122118
```shell
123119
sudo systemctl status syscapture
124120
```
125121

126-
This setup ensures that SysCapture runs continuously in the background, even after reboots.
122+
This configuration ensures SysCapture runs continuously in the background and restarts automatically if it fails or after a reboot.
127123

128124
---
129125

130126
## API Documentation
131127

132-
SysCapture’s API is documented using the OpenAPI specification.
133-
View the complete [OpenAPI Spec](https://github.com/nodebytehosting/syscapture/blob/master/openapi.yml) for details on endpoints and usage.
128+
SysCapture’s API is fully documented via the OpenAPI specification.
129+
See the complete [OpenAPI Spec](https://github.com/nodebytehosting/syscapture/blob/master/openapi.yml) for details on endpoints and usage.
134130

135131
---
136132

137133
## Contributing
138134

139-
Contributions are welcome! To get started:
135+
Contributions are welcome! To contribute:
140136

141137
1. Fork the repository.
142-
2. Create a new branch: `git checkout -b feature/your-feature`.
143-
3. Commit your changes: `git commit -m 'Add feature'`.
144-
4. Push to the branch: `git push origin feature/your-feature`.
138+
2. Create a new branch (`git checkout -b feature/your-feature`).
139+
3. Commit your changes (`git commit -m 'Add feature'`).
140+
4. Push the branch (`git push origin feature/your-feature`).
145141
5. Open a pull request.
146142

147-
Please follow the project's coding conventions and include tests for your changes when applicable.
143+
Please follow our coding conventions and include tests where applicable.
148144

149145
---

internal/handler/health.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package handler
22

33
import (
4-
"github.com/gin-gonic/gin"
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
57
)
68

9+
// Health responds with a JSON object indicating the service status.
10+
// It can be used as a basic health check endpoint.
711
func Health(c *gin.Context) {
8-
c.JSON(200, "OK")
9-
}
12+
c.JSON(http.StatusOK, gin.H{
13+
"status": "ok",
14+
"message": "Service is healthy",
15+
})
16+
}

internal/handler/metrics.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,51 @@
11
package handler
22

33
import (
4+
"net/http"
5+
46
"github.com/gin-gonic/gin"
57
"github.com/nodebytehosting/syscapture/internal/metric"
68
)
79

10+
// handleMetricResponse writes the appropriate HTTP status and JSON response.
11+
// If one or more errors are present, it uses HTTP StatusMultiStatus (207).
812
func handleMetricResponse(c *gin.Context, metrics metric.Metric, errs []metric.CustomErr) {
9-
statusCode := 200
10-
if len(errs) > 0 {
11-
statusCode = 207
12-
}
13-
c.JSON(statusCode, metric.APIResponse{
14-
Data: metrics,
15-
Errors: errs,
16-
})
13+
statusCode := http.StatusOK
14+
if len(errs) > 0 {
15+
statusCode = http.StatusMultiStatus
16+
}
17+
c.JSON(statusCode, metric.APIResponse{
18+
Data: metrics,
19+
Errors: errs,
20+
})
1721
}
1822

23+
// Metrics aggregates all system metrics and sends the response.
1924
func Metrics(c *gin.Context) {
20-
metrics, metricsErrs := metric.GetAllSystemMetrics()
21-
handleMetricResponse(c, metrics, metricsErrs)
25+
metrics, metricsErrs := metric.GetAllSystemMetrics()
26+
handleMetricResponse(c, metrics, metricsErrs)
2227
}
2328

29+
// MetricsCPU returns CPU metrics.
2430
func MetricsCPU(c *gin.Context) {
25-
cpuMetrics, metricsErrs := metric.CollectCPUMetrics()
26-
handleMetricResponse(c, cpuMetrics, metricsErrs)
31+
cpuMetrics, metricsErrs := metric.CollectCPUMetrics()
32+
handleMetricResponse(c, cpuMetrics, metricsErrs)
2733
}
2834

35+
// MetricsMemory returns memory metrics.
2936
func MetricsMemory(c *gin.Context) {
30-
memoryMetrics, metricsErrs := metric.CollectMemoryMetrics()
31-
handleMetricResponse(c, memoryMetrics, metricsErrs)
37+
memoryMetrics, metricsErrs := metric.CollectMemoryMetrics()
38+
handleMetricResponse(c, memoryMetrics, metricsErrs)
3239
}
3340

41+
// MetricsDisk returns disk metrics.
3442
func MetricsDisk(c *gin.Context) {
35-
diskMetrics, metricsErrs := metric.CollectDiskMetrics()
36-
handleMetricResponse(c, diskMetrics, metricsErrs)
43+
diskMetrics, metricsErrs := metric.CollectDiskMetrics()
44+
handleMetricResponse(c, diskMetrics, metricsErrs)
3745
}
3846

47+
// MetricsHost returns host information metrics.
3948
func MetricsHost(c *gin.Context) {
40-
hostMetrics, metricsErrs := metric.GetHostInformation()
41-
handleMetricResponse(c, hostMetrics, metricsErrs)
42-
}
49+
hostMetrics, metricsErrs := metric.GetHostInformation()
50+
handleMetricResponse(c, hostMetrics, metricsErrs)
51+
}

0 commit comments

Comments
 (0)