-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
executable file
·149 lines (122 loc) · 4.29 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"sync"
pb "github.com/accuknox/KubeArmor/protobuf"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
)
var (
totalAlertsRequestsinHost = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kubearmor_relay_logs_in_host_total",
Help: "Total number of logs generated from Kubearmor Relay based on HostName",
}, []string{"HostName"})
totalAlertsRequestsinPod = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kubearmor_relay_logs_in_pod_total",
Help: "Total number of logs generated from Kubearmor Relay based on PodName",
}, []string{"PodName"})
totalAlertsRequestsinNamespace = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kubearmor_relay_logs_in_namespace_total",
Help: "Total number of logs generated from Kubearmor Relay based on Namespace",
}, []string{"NamespaceName"})
totalAlertsRequestsinContainer = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kubearmor_relay_logs_in_container_total",
Help: "Total number of logs generated from Kubearmor Relay based on Container",
}, []string{"ContainerName"})
totalAlertsWithPolicy = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kubearmor_relay_policy_logs_total",
Help: "Total number of logs generated on a given policy",
}, []string{"PolicyName"})
totalAlertsWithSeverity = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kubearmor_relay_logs_with_severity_total",
Help: "Total number of logs generated with X severity or above",
}, []string{"Severity"})
totalAlertsWithType = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kubearmor_relay_logs_with_type_total",
Help: "Total number of logs generated from Kubearmor Relay based on given type",
}, []string{"Type"})
totalAlertsWithOperation = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kubearmor_relay_logs_with_operation_total",
Help: "Total number of logs generated with a given Operation",
}, []string{"Operation"})
totalAlertsWithAction = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kubearmor_relay_logs_with_action_total",
Help: "Total number of logs generated with a given action",
}, []string{"Action"})
)
func init() {
prometheus.MustRegister(totalAlertsRequestsinHost)
prometheus.MustRegister(totalAlertsRequestsinPod)
prometheus.MustRegister(totalAlertsRequestsinNamespace)
prometheus.MustRegister(totalAlertsRequestsinContainer)
prometheus.MustRegister(totalAlertsWithPolicy)
prometheus.MustRegister(totalAlertsWithSeverity)
prometheus.MustRegister(totalAlertsWithType)
prometheus.MustRegister(totalAlertsWithOperation)
prometheus.MustRegister(totalAlertsWithAction)
}
func GetPrometheusAlerts(wg *sync.WaitGroup) {
url := "kubearmor.kube-system.svc.cluster.local"
port := "32767"
address := url + ":" + port
connection, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
fmt.Println(err)
}
client := pb.NewLogServiceClient(connection)
req := &pb.RequestMessage{
Filter: "policy",
}
stream, err := client.WatchAlerts(context.Background(), req)
if err != nil {
fmt.Printf("Failed to call WatchAlerts() (%s)\n", err.Error())
}
for {
alertIn, err := stream.Recv()
if err != nil {
fmt.Printf("Failed to receive any alerts (%s)\n", err.Error())
break
}
switch err {
case io.EOF:
fmt.Println(err.Error())
break
case nil:
default:
fmt.Println(err.Error())
}
fmt.Println(alertIn)
totalAlertsRequestsinHost.WithLabelValues(alertIn.HostName).Add(1)
totalAlertsRequestsinPod.WithLabelValues(alertIn.PodName).Add(1)
totalAlertsRequestsinNamespace.WithLabelValues(alertIn.NamespaceName).Add(1)
totalAlertsRequestsinContainer.WithLabelValues(alertIn.ContainerName).Add(1)
totalAlertsWithPolicy.WithLabelValues(alertIn.PolicyName).Add(1)
totalAlertsWithSeverity.WithLabelValues(alertIn.Severity).Add(1)
totalAlertsWithType.WithLabelValues(alertIn.Type).Add(1)
totalAlertsWithOperation.WithLabelValues(alertIn.Operation).Add(1)
totalAlertsWithAction.WithLabelValues(alertIn.Action).Add(1)
}
wg.Done()
}
func main() {
var wg sync.WaitGroup
wg.Add(1)
go GetPrometheusAlerts(&wg)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":9100", nil))
wg.Wait()
}