Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.

Commit 91caf75

Browse files
Add option to move deleted devices to a dedicated group (#11)
1 parent 58c4367 commit 91caf75

File tree

6 files changed

+156
-12
lines changed

6 files changed

+156
-12
lines changed

argus/argus.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,25 @@ func createServiceDeviceGroup(argus *Argus, parentDeviceGroup *lmv1.RestDeviceGr
219219
return
220220
}
221221

222+
func createServiceDeletedDeviceGroup(argus *Argus, parentDeviceGroup *lmv1.RestDeviceGroup) (clusterDeviceGroup *lmv1.RestDeviceGroup, err error) {
223+
name := "_deleted"
224+
appliesTo := "hasCategory(\"" + constants.NodeDeletedCategory + "\") && auto.clustername ==\"" + argus.Config.ClusterName + "\""
225+
226+
clusterDeviceGroup, err = findDeviceGroup(argus.LMClient, parentDeviceGroup.Id, name)
227+
if err != nil {
228+
return
229+
}
230+
231+
if clusterDeviceGroup == nil {
232+
clusterDeviceGroup, err = createDeviceGroup(argus.LMClient, name, appliesTo, argus.Config.DisableAlerting, parentDeviceGroup.Id)
233+
if err != nil {
234+
return
235+
}
236+
}
237+
238+
return
239+
}
240+
222241
func createNodeDeviceGroup(argus *Argus, parentDeviceGroup *lmv1.RestDeviceGroup) (clusterDeviceGroup *lmv1.RestDeviceGroup, err error) {
223242
name := "Nodes"
224243
appliesTo := "hasCategory(\"" + constants.NodeCategory + "\") && auto.clustername ==\"" + argus.Config.ClusterName + "\""
@@ -238,6 +257,25 @@ func createNodeDeviceGroup(argus *Argus, parentDeviceGroup *lmv1.RestDeviceGroup
238257
return
239258
}
240259

260+
func createNodeDeletedDeviceGroup(argus *Argus, parentDeviceGroup *lmv1.RestDeviceGroup) (clusterDeviceGroup *lmv1.RestDeviceGroup, err error) {
261+
name := "_deleted"
262+
appliesTo := "hasCategory(\"" + constants.NodeDeletedCategory + "\") && auto.clustername ==\"" + argus.Config.ClusterName + "\""
263+
264+
clusterDeviceGroup, err = findDeviceGroup(argus.LMClient, parentDeviceGroup.Id, name)
265+
if err != nil {
266+
return
267+
}
268+
269+
if clusterDeviceGroup == nil {
270+
clusterDeviceGroup, err = createDeviceGroup(argus.LMClient, name, appliesTo, true, parentDeviceGroup.Id)
271+
if err != nil {
272+
return
273+
}
274+
}
275+
276+
return
277+
}
278+
241279
func createPodDeviceGroup(argus *Argus, parentDeviceGroup *lmv1.RestDeviceGroup) (clusterDeviceGroup *lmv1.RestDeviceGroup, err error) {
242280
name := "Pods"
243281
appliesTo := ""
@@ -257,6 +295,25 @@ func createPodDeviceGroup(argus *Argus, parentDeviceGroup *lmv1.RestDeviceGroup)
257295
return
258296
}
259297

298+
func createPodDeletedDeviceGroup(argus *Argus, parentDeviceGroup *lmv1.RestDeviceGroup) (clusterDeviceGroup *lmv1.RestDeviceGroup, err error) {
299+
name := "_deleted"
300+
appliesTo := "hasCategory(\"" + constants.PodDeletedCategory + "\") && auto.clustername ==\"" + argus.Config.ClusterName + "\""
301+
302+
clusterDeviceGroup, err = findDeviceGroup(argus.LMClient, parentDeviceGroup.Id, name)
303+
if err != nil {
304+
return
305+
}
306+
307+
if clusterDeviceGroup == nil {
308+
clusterDeviceGroup, err = createDeviceGroup(argus.LMClient, name, appliesTo, true, parentDeviceGroup.Id)
309+
if err != nil {
310+
return
311+
}
312+
}
313+
314+
return
315+
}
316+
260317
func createDeviceGroups(argus *Argus) (deviceGroups map[string]int32, err error) {
261318
deviceGroups = make(map[string]int32)
262319
clusterDeviceGroup, err := createClusterDeviceGroup(argus)
@@ -271,19 +328,31 @@ func createDeviceGroups(argus *Argus) (deviceGroups map[string]int32, err error)
271328
}
272329
deviceGroups["services"] = serviceDeviceGroup.Id
273330
log.Infof("Using service device group with id %d", serviceDeviceGroup.Id)
331+
_, err = createServiceDeletedDeviceGroup(argus, serviceDeviceGroup)
332+
if err != nil {
333+
return
334+
}
274335

275336
nodeDeviceGroup, err := createNodeDeviceGroup(argus, clusterDeviceGroup)
276337
if err != nil {
277338
return
278339
}
279340
log.Infof("Using node device group with id %d", nodeDeviceGroup.Id)
341+
_, err = createNodeDeletedDeviceGroup(argus, nodeDeviceGroup)
342+
if err != nil {
343+
return
344+
}
280345

281346
podDeviceGroup, err := createPodDeviceGroup(argus, clusterDeviceGroup)
282347
if err != nil {
283348
return
284349
}
285350
deviceGroups["pods"] = podDeviceGroup.Id
286351
log.Infof("Using pod device group with id %d", podDeviceGroup.Id)
352+
_, err = createPodDeletedDeviceGroup(argus, podDeviceGroup)
353+
if err != nil {
354+
return
355+
}
287356

288357
return
289358
}

argus/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Config struct {
1111
Key string
1212
Debug bool
1313
DisableAlerting bool
14+
DeleteDevices bool
1415
PreferredCollector int32
1516
}
1617

@@ -25,6 +26,7 @@ func GetConfig() *Config {
2526
company := viper.GetString("company")
2627
debug := viper.GetBool("debug")
2728
disableAlerting := viper.GetBool("disable_alerting")
29+
deleteDevices := viper.GetBool("delete_devices")
2830
id := viper.GetString("id")
2931
key := viper.GetString("key")
3032
preferredCollector := int32(viper.GetInt("preferred_collector"))
@@ -35,6 +37,7 @@ func GetConfig() *Config {
3537
Company: company,
3638
Debug: debug,
3739
DisableAlerting: disableAlerting,
40+
DeleteDevices: deleteDevices,
3841
ID: id,
3942
Key: key,
4043
PreferredCollector: preferredCollector,

argus/watch/node/node.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,31 @@ func (w Watcher) DeleteFunc() func(obj interface{}) {
8484
}
8585
if restResponse.Data.Total == 1 {
8686
id := restResponse.Data.Items[0].Id
87-
log.Infof("Deleting node %s with id %d", node.Name, id)
88-
restNullObjectResponse, _, err := w.LMClient.DeleteDevice(id)
89-
if err != nil {
90-
log.Printf("Failed to delete device with id %q: %s", id, restNullObjectResponse.Errmsg)
87+
if w.Config.DeleteDevices {
88+
log.Infof("Deleting node %s with id %d", node.Name, id)
89+
restNullObjectResponse, _, err := w.LMClient.DeleteDevice(id)
90+
if err != nil {
91+
log.Printf("Failed to delete device with id %q: %s", id, restNullObjectResponse.Errmsg)
92+
}
93+
} else {
94+
log.Infof("Moving node %s with id %d to deleted group", node.Name, id)
95+
categories := constants.NodeDeletedCategory
96+
for k, v := range node.Labels {
97+
categories += "," + k + "=" + v
98+
99+
}
100+
device := lmv1.RestDevice{
101+
CustomProperties: []lmv1.NameAndValue{
102+
{
103+
Name: "system.categories",
104+
Value: categories,
105+
},
106+
},
107+
}
108+
restResponse, _, err := w.LMClient.PatchDeviceById(device, id, "replace", "customProperties")
109+
if err != nil {
110+
log.Errorf("Failed to patch node %s: %s", node.Name, restResponse.Errmsg)
111+
}
91112
}
92113
}
93114
}

argus/watch/pod/pod.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,32 @@ func (w Watcher) DeleteFunc() func(obj interface{}) {
8484
}
8585
if restResponse.Data.Total == 1 {
8686
id := restResponse.Data.Items[0].Id
87-
log.Infof("Deleting pod %s with id %d", pod.Name, id)
88-
w.LMClient.DeleteDevice(id)
87+
if w.Config.DeleteDevices {
88+
log.Infof("Deleting pod %s with id %d", pod.Name, id)
89+
restNullObjectResponse, _, err := w.LMClient.DeleteDevice(id)
90+
if err != nil {
91+
log.Printf("Failed to delete device with id %q: %s", id, restNullObjectResponse.Errmsg)
92+
}
93+
} else {
94+
log.Infof("Moving pod %s with id %d to deleted group", pod.Name, id)
95+
categories := constants.PodDeletedCategory
96+
for k, v := range pod.Labels {
97+
categories += "," + k + "=" + v
98+
99+
}
100+
device := lmv1.RestDevice{
101+
CustomProperties: []lmv1.NameAndValue{
102+
{
103+
Name: "system.categories",
104+
Value: categories,
105+
},
106+
},
107+
}
108+
restResponse, _, err := w.LMClient.PatchDeviceById(device, id, "replace", "customProperties")
109+
if err != nil {
110+
log.Errorf("Failed to patch pod %s: %s", pod.Name, restResponse.Errmsg)
111+
}
112+
}
89113
}
90114
}
91115
}

argus/watch/service/service.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,32 @@ func (w Watcher) DeleteFunc() func(obj interface{}) {
9494
}
9595
if restResponse.Data.Total == 1 {
9696
id := restResponse.Data.Items[0].Id
97-
log.Infof("Deleting service %s with id %d", service.Name, id)
98-
w.LMClient.DeleteDevice(id)
97+
if w.Config.DeleteDevices {
98+
log.Infof("Deleting service %s with id %d", service.Name, id)
99+
restNullObjectResponse, _, err := w.LMClient.DeleteDevice(id)
100+
if err != nil {
101+
log.Printf("Failed to delete device with id %q: %s", id, restNullObjectResponse.Errmsg)
102+
}
103+
} else {
104+
log.Infof("Moving service %s with id %d to deleted group", service.Name, id)
105+
categories := constants.ServiceDeletedCategory
106+
for k, v := range service.Labels {
107+
categories += "," + k + "=" + v
108+
109+
}
110+
device := lmv1.RestDevice{
111+
CustomProperties: []lmv1.NameAndValue{
112+
{
113+
Name: "system.categories",
114+
Value: categories,
115+
},
116+
},
117+
}
118+
restResponse, _, err := w.LMClient.PatchDeviceById(device, id, "replace", "customProperties")
119+
if err != nil {
120+
log.Errorf("Failed to patch service %s: %s", service.Name, restResponse.Errmsg)
121+
}
122+
}
99123
}
100124
}
101125
}

constants/constants.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package constants
22

33
const (
4-
ClusterCategory = "KubernetesCluster"
5-
NodeCategory = "KubernetesNode"
6-
ServiceCategory = "KubernetesService"
7-
PodCategory = "KubernetesPod"
4+
ClusterCategory = "KubernetesCluster"
5+
NodeCategory = "KubernetesNode"
6+
NodeDeletedCategory = "KubernetesNodeDeleted"
7+
ServiceCategory = "KubernetesService"
8+
ServiceDeletedCategory = "KubernetesServiceDeleted"
9+
PodCategory = "KubernetesPod"
10+
PodDeletedCategory = "KubernetesPodDeleted"
811
)
912

1013
var (

0 commit comments

Comments
 (0)