Implement CheckCle Microservice Operation

A Go-based microservice for service operations including ICMP ping, DNS resolution, and TCP connectivity
This commit is contained in:
Tola Leng
2025-06-19 17:49:30 +07:00
parent 89572c44b7
commit d3ea5dc77c
36 changed files with 2895 additions and 0 deletions
@@ -0,0 +1,66 @@
package savers
import (
"fmt"
"strconv"
"time"
"service-operation/pocketbase"
"service-operation/types"
)
func (ms *MetricsSaver) SaveTCPDataToPocketBase(result *types.OperationResult, serviceID string) {
// Create a short, professional status message
var details string
if result.Success && result.TCPConnected {
// Success message with connection info
details = fmt.Sprintf("✅ TCP Connection OK - Port %d accessible", result.Port)
// Add response time
details += fmt.Sprintf(" | Connection time: %.2fms",
float64(result.ResponseTime.Nanoseconds())/1000000)
} else {
// Error message with port info
details = fmt.Sprintf("❌ TCP Connection Failed - Port %d unreachable", result.Port)
if result.Error != "" {
details += fmt.Sprintf(" (%s)", GetShortErrorMessage(result.Error))
}
// Add response time if available
if result.ResponseTime > 0 {
details += fmt.Sprintf(" | Timeout: %.2fms",
float64(result.ResponseTime.Nanoseconds())/1000000)
}
}
connectionStatus := "disconnected"
if result.TCPConnected {
connectionStatus = "connected"
}
tcpData := pocketbase.TCPDataRecord{
ServiceID: serviceID,
Timestamp: time.Now(),
ResponseTime: result.ResponseTime.Milliseconds(),
Status: GetStatusString(result.Success),
Connection: connectionStatus,
Latency: fmt.Sprintf("%.2fms", float64(result.ResponseTime.Nanoseconds())/1000000),
Port: strconv.Itoa(result.Port),
ErrorMessage: result.Error,
Details: details,
RegionName: "default",
AgentID: "1",
}
if err := ms.pbClient.SaveTCPData(tcpData); err != nil {
fmt.Printf("Failed to save TCP data to PocketBase: %v\n", err)
}
}
// Method for monitoring service usage
func (ms *MetricsSaver) SaveTCPDataForService(service pocketbase.Service, result *types.OperationResult) {
ms.SaveTCPDataToPocketBase(result, service.ID)
}