/
datarow.go
1370 lines (1281 loc) · 39.3 KB
/
datarow.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"crypto/sha256"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/lkarlslund/stringdedup"
)
const ListSepChar1 = "\x00"
// DataRow represents a single entry in a DataTable
type DataRow struct {
noCopy noCopy // we don't want to make copies, use references
DataStore *DataStore // reference to the datastore itself
Refs map[TableName]*DataRow // contains references to other objects, ex.: hosts from the services table
LastUpdate int64 // timestamp when this row has been updated
dataString []string // stores string data
dataInt []int // stores integers
dataInt64 []int64 // stores large integers
dataFloat []float64 // stores floats
dataStringList [][]string // stores stringlists
dataInt64List [][]int64 // stores lists of integers
dataServiceMemberList [][]ServiceMember // stores list of servicemembers
dataStringLarge []StringContainer // stores large strings
dataInterfaceList [][]interface{}
}
// NewDataRow creates a new DataRow
func NewDataRow(store *DataStore, raw *[]interface{}, columns *ColumnList, timestamp int64) (d *DataRow, err error) {
d = &DataRow{
LastUpdate: timestamp,
DataStore: store,
}
if raw == nil {
// virtual tables without data have no references or ids
return
}
if !store.Table.PassthroughOnly && len(store.Table.RefTables) > 0 {
d.Refs = make(map[TableName]*DataRow, len(store.Table.RefTables))
}
err = d.SetData(raw, columns, timestamp)
if err != nil {
return
}
d.setLowerCaseCache()
err = d.setReferences()
return
}
// GetID calculates and returns the ID value (nul byte concatenated primary key values)
func (d *DataRow) GetID() string {
if len(d.DataStore.Table.PrimaryKey) == 0 {
return ""
}
if len(d.DataStore.Table.PrimaryKey) == 1 {
id := d.GetStringByName(d.DataStore.Table.PrimaryKey[0])
if *id == "" {
log.Errorf("[%s] id for %s is null", d.DataStore.Peer.Name, d.DataStore.Table.Name.String())
}
return *id
}
var key strings.Builder
for i, k := range d.DataStore.Table.PrimaryKey {
if i > 0 {
key.WriteString(ListSepChar1)
}
key.WriteString(*(d.GetStringByName(k)))
}
id := key.String()
if id == "" || id == ListSepChar1 {
log.Errorf("[%s] id for %s is null", d.DataStore.Peer.Name, d.DataStore.Table.Name.String())
}
return id
}
// GetID2 returns the 2 strings for tables with 2 primary keys
func (d *DataRow) GetID2() (string, string) {
id1 := d.GetStringByName(d.DataStore.Table.PrimaryKey[0])
if *id1 == "" {
log.Errorf("[%s] id1 for %s is null", d.DataStore.Peer.Name, d.DataStore.Table.Name.String())
}
id2 := d.GetStringByName(d.DataStore.Table.PrimaryKey[1])
if *id2 == "" {
log.Errorf("[%s] id2 for %s is null", d.DataStore.Peer.Name, d.DataStore.Table.Name.String())
}
return *id1, *id2
}
// setData creates initial data
func (d *DataRow) SetData(raw *[]interface{}, columns *ColumnList, timestamp int64) error {
d.dataString = make([]string, d.DataStore.DataSizes[StringCol])
d.dataStringList = make([][]string, d.DataStore.DataSizes[StringListCol])
d.dataInt = make([]int, d.DataStore.DataSizes[IntCol])
d.dataInt64 = make([]int64, d.DataStore.DataSizes[Int64Col])
d.dataInt64List = make([][]int64, d.DataStore.DataSizes[Int64ListCol])
d.dataFloat = make([]float64, d.DataStore.DataSizes[FloatCol])
d.dataServiceMemberList = make([][]ServiceMember, d.DataStore.DataSizes[ServiceMemberListCol])
d.dataInterfaceList = make([][]interface{}, d.DataStore.DataSizes[InterfaceListCol])
d.dataStringLarge = make([]StringContainer, d.DataStore.DataSizes[StringLargeCol])
return d.UpdateValues(0, raw, columns, timestamp)
}
// setLowerCaseCache sets lowercase columns
func (d *DataRow) setLowerCaseCache() {
for from, to := range d.DataStore.LowerCaseColumns {
d.dataString[to] = strings.ToLower(d.dataString[from])
}
}
// setReferences creates reference entries for cross referenced objects
func (d *DataRow) setReferences() (err error) {
store := d.DataStore
for i := range store.Table.RefTables {
ref := store.Table.RefTables[i]
tableName := ref.Table.Name
refsByName := store.DataSet.tables[tableName].Index
refsByName2 := store.DataSet.tables[tableName].Index2
switch len(ref.Columns) {
case 1:
d.Refs[tableName] = refsByName[*(d.GetString(ref.Columns[0]))]
case 2:
d.Refs[tableName] = refsByName2[*(d.GetString(ref.Columns[0]))][*(d.GetString(ref.Columns[1]))]
}
if d.Refs[tableName] == nil {
if tableName == TableServices && (store.Table.Name == TableComments || store.Table.Name == TableDowntimes) {
// this may happen for optional reference columns, ex. services in comments
continue
}
return fmt.Errorf("%s reference not found from table %s, refmap contains %d elements", tableName.String(), store.Table.Name.String(), len(refsByName))
}
}
return
}
// GetColumn returns the column by name
func (d *DataRow) GetColumn(name string) *Column {
return d.DataStore.Table.ColumnsIndex[name]
}
// GetString returns the string value for given column
func (d *DataRow) GetString(col *Column) *string {
switch col.StorageType {
case LocalStore:
switch col.DataType {
case StringCol:
return &(d.dataString[col.Index])
case IntCol:
val := fmt.Sprintf("%d", d.dataInt[col.Index])
return &val
case Int64Col:
val := strconv.FormatInt(d.dataInt64[col.Index], 10)
return &val
case FloatCol:
val := fmt.Sprintf("%v", d.dataFloat[col.Index])
return &val
case StringLargeCol:
return d.dataStringLarge[col.Index].StringRef()
case StringListCol:
return joinStringlist(&d.dataStringList[col.Index], ListSepChar1)
case ServiceMemberListCol:
val := fmt.Sprintf("%v", d.dataServiceMemberList[col.Index])
return &val
case InterfaceListCol:
val := fmt.Sprintf("%v", d.dataInterfaceList[col.Index])
return &val
case Int64ListCol:
val := strings.Join(strings.Fields(fmt.Sprint(d.dataInt64List[col.Index])), ListSepChar1)
return &val
default:
log.Panicf("unsupported type: %s", col.DataType)
}
case RefStore:
ref := d.Refs[col.RefColTableName]
if ref == nil {
return interface2stringNoDedup(col.GetEmptyValue())
}
return ref.GetString(col.RefCol)
case VirtualStore:
return interface2stringNoDedup(d.getVirtualRowValue(col))
}
panic(fmt.Sprintf("unsupported type: %s", col.DataType))
}
// GetStringByName returns the string value for given column name
func (d *DataRow) GetStringByName(name string) *string {
return d.GetString(d.DataStore.Table.ColumnsIndex[name])
}
// GetStringList returns the string list for given column
func (d *DataRow) GetStringList(col *Column) *[]string {
switch col.StorageType {
case LocalStore:
if col.DataType == StringListCol {
return &(d.dataStringList[col.Index])
}
log.Panicf("unsupported type: %s", col.DataType)
case RefStore:
ref := d.Refs[col.RefColTableName]
if ref == nil {
return interface2stringlist(col.GetEmptyValue())
}
return ref.GetStringList(col.RefCol)
case VirtualStore:
return interface2stringlist(d.getVirtualRowValue(col))
}
panic(fmt.Sprintf("unsupported type: %s", col.DataType))
}
// GetStringListByName returns the string list for given column name
func (d *DataRow) GetStringListByName(name string) *[]string {
return d.GetStringList(d.DataStore.Table.ColumnsIndex[name])
}
// GetFloat returns the float64 value for given column
func (d *DataRow) GetFloat(col *Column) float64 {
switch col.StorageType {
case LocalStore:
switch col.DataType {
case FloatCol:
return d.dataFloat[col.Index]
case IntCol:
return float64(d.dataInt[col.Index])
case Int64Col:
return float64(d.dataInt64[col.Index])
default:
log.Panicf("unsupported type: %s", col.DataType)
}
case RefStore:
ref := d.Refs[col.RefColTableName]
if ref == nil {
return interface2float64(col.GetEmptyValue())
}
return ref.GetFloat(col.RefCol)
case VirtualStore:
return interface2float64(d.getVirtualRowValue(col))
}
panic(fmt.Sprintf("unsupported type: %s", col.DataType))
}
// GetInt returns the int value for given column
func (d *DataRow) GetInt(col *Column) int {
switch col.StorageType {
case LocalStore:
switch col.DataType {
case IntCol:
return d.dataInt[col.Index]
case FloatCol:
return int(d.dataFloat[col.Index])
default:
log.Panicf("unsupported type: %s", col.DataType)
}
case RefStore:
ref := d.Refs[col.RefColTableName]
if ref == nil {
return interface2int(col.GetEmptyValue())
}
return ref.GetInt(col.RefCol)
case VirtualStore:
return interface2int(d.getVirtualRowValue(col))
}
panic(fmt.Sprintf("unsupported type: %s", col.StorageType))
}
// GetInt returns the int64 value for given column
func (d *DataRow) GetInt64(col *Column) int64 {
switch col.StorageType {
case LocalStore:
switch col.DataType {
case Int64Col:
return d.dataInt64[col.Index]
case IntCol:
return int64(d.dataInt[col.Index])
case FloatCol:
return int64(d.dataFloat[col.Index])
default:
log.Panicf("unsupported type: %s", col.DataType)
}
case RefStore:
ref := d.Refs[col.RefColTableName]
if ref == nil {
return interface2int64(col.GetEmptyValue())
}
return ref.GetInt64(col.RefCol)
case VirtualStore:
return interface2int64(d.getVirtualRowValue(col))
}
panic(fmt.Sprintf("unsupported type: %s", col.StorageType))
}
// GetIntByName returns the int value for given column name
func (d *DataRow) GetIntByName(name string) int {
return d.GetInt(d.DataStore.Table.ColumnsIndex[name])
}
// GetInt64ByName returns the int value for given column name
func (d *DataRow) GetInt64ByName(name string) int64 {
return d.GetInt64(d.DataStore.Table.ColumnsIndex[name])
}
// GetInt64List returns the int64 list for given column
func (d *DataRow) GetInt64List(col *Column) []int64 {
switch col.StorageType {
case LocalStore:
if col.DataType == Int64ListCol {
return d.dataInt64List[col.Index]
}
log.Panicf("unsupported type: %s", col.DataType)
case RefStore:
ref := d.Refs[col.RefColTableName]
if ref == nil {
return interface2int64list(col.GetEmptyValue())
}
return ref.GetInt64List(col.RefCol)
case VirtualStore:
return interface2int64list(d.getVirtualRowValue(col))
}
panic(fmt.Sprintf("unsupported type: %s", col.StorageType))
}
// GetHashMap returns the hashmap for given column
func (d *DataRow) GetHashMap(col *Column) map[string]string {
switch col.StorageType {
case LocalStore:
log.Panicf("unsupported type: %s", col.DataType)
case RefStore:
ref := d.Refs[col.RefColTableName]
if ref == nil {
return interface2hashmap(col.GetEmptyValue())
}
return ref.GetHashMap(col.RefCol)
case VirtualStore:
return interface2hashmap(d.getVirtualRowValue(col))
}
panic(fmt.Sprintf("unsupported type: %s", col.StorageType))
}
// GetServiceMemberList returns the a list of service members
func (d *DataRow) GetServiceMemberList(col *Column) *[]ServiceMember {
switch col.StorageType {
case LocalStore:
if col.DataType == ServiceMemberListCol {
return &(d.dataServiceMemberList[col.Index])
}
log.Panicf("unsupported type: %s", col.DataType)
case RefStore:
ref := d.Refs[col.RefColTableName]
if ref == nil {
return interface2servicememberlist(col.GetEmptyValue())
}
return ref.GetServiceMemberList(col.RefCol)
case VirtualStore:
return interface2servicememberlist(d.getVirtualRowValue(col))
}
panic(fmt.Sprintf("unsupported type: %s", col.StorageType))
}
// GetInterfaceList returns the a list of interfaces
func (d *DataRow) GetInterfaceList(col *Column) []interface{} {
switch col.StorageType {
case LocalStore:
if col.DataType == InterfaceListCol {
return d.dataInterfaceList[col.Index]
}
log.Panicf("unsupported type: %s", col.DataType)
case RefStore:
ref := d.Refs[col.RefColTableName]
if ref == nil {
return interface2interfacelist(col.GetEmptyValue())
}
return ref.GetInterfaceList(col.RefCol)
case VirtualStore:
return interface2interfacelist(d.getVirtualRowValue(col))
}
panic(fmt.Sprintf("unsupported type: %s", col.StorageType))
}
// GetValueByColumn returns the raw value for given column
func (d *DataRow) GetValueByColumn(col *Column) interface{} {
if col.Optional != NoFlags && !d.DataStore.Peer.HasFlag(col.Optional) {
return col.GetEmptyValue()
}
switch col.StorageType {
case LocalStore:
switch col.DataType {
case StringCol:
return d.dataString[col.Index]
case StringListCol:
return d.dataStringList[col.Index]
case StringLargeCol:
return d.dataStringLarge[col.Index].StringRef()
case IntCol:
return d.dataInt[col.Index]
case Int64ListCol:
return d.dataInt64List[col.Index]
case Int64Col:
return d.dataInt64[col.Index]
case FloatCol:
return d.dataFloat[col.Index]
case InterfaceListCol:
return d.dataInterfaceList[col.Index]
case ServiceMemberListCol:
return d.dataServiceMemberList[col.Index]
default:
log.Panicf("unsupported column %s (type %s) in table %s", col.Name, col.DataType.String(), d.DataStore.Table.Name.String())
}
case RefStore:
return d.Refs[col.RefColTableName].GetValueByColumn(col.RefCol)
case VirtualStore:
return d.getVirtualRowValue(col)
}
panic(fmt.Sprintf("unsupported type: %s", col.StorageType))
}
// getVirtualRowValue returns the actual value for a virtual column.
func (d *DataRow) getVirtualRowValue(col *Column) interface{} {
var value interface{}
if col.VirtualMap.StatusKey > 0 {
if d.DataStore.Peer == nil {
log.Panicf("requesting column '%s' from table '%s' with peer", col.Name, d.DataStore.Table.Name)
}
p := d.DataStore.Peer
ok := false
if p.HasFlag(LMDSub) {
value, ok = d.getVirtualSubLMDValue(col)
}
if !ok {
switch d.DataStore.PeerLockMode {
case PeerLockModeFull:
value = p.Status[col.VirtualMap.StatusKey]
case PeerLockModeSimple:
switch col.VirtualMap.StatusKey {
case PeerName:
return &(p.Name)
case PeerKey:
return &(p.ID)
default:
value = p.StatusGet(col.VirtualMap.StatusKey)
}
}
}
} else {
value = col.VirtualMap.ResolveFunc(d, col)
}
return cast2Type(value, col)
}
// VirtualColLocaltime returns current unix timestamp
func VirtualColLocaltime(d *DataRow, col *Column) interface{} {
return float64(time.Now().UnixNano()) / float64(time.Second)
}
// VirtualColLastStateChangeOrder returns sortable state
func VirtualColLastStateChangeOrder(d *DataRow, col *Column) interface{} {
// return last_state_change or program_start
lastStateChange := d.GetIntByName("last_state_change")
if lastStateChange == 0 {
return d.DataStore.Peer.Status[ProgramStart]
}
return lastStateChange
}
// VirtualColStateOrder returns sortable state
func VirtualColStateOrder(d *DataRow, col *Column) interface{} {
// return 4 instead of 2, which makes critical come first
// this way we can use this column to sort by state
state := d.GetIntByName("state")
if state == 2 {
return 4
}
return state
}
// VirtualColHasLongPluginOutput returns 1 if there is long plugin output, 0 if not
func VirtualColHasLongPluginOutput(d *DataRow, col *Column) interface{} {
val := d.GetStringByName("long_plugin_output")
if *val != "" {
return 1
}
return 0
}
// VirtualColServicesWithInfo returns list of services with additional information
func VirtualColServicesWithInfo(d *DataRow, col *Column) interface{} {
services := d.GetStringListByName("services")
hostName := d.GetStringByName("name")
servicesStore := d.DataStore.DataSet.tables[TableServices]
stateCol := servicesStore.Table.GetColumn("state")
checkedCol := servicesStore.Table.GetColumn("has_been_checked")
outputCol := servicesStore.Table.GetColumn("plugin_output")
res := make([]interface{}, 0)
for i := range *services {
service, ok := servicesStore.Index2[*hostName][(*services)[i]]
if !ok {
log.Errorf("Could not find service: %s - %s\n", *hostName, (*services)[i])
continue
}
serviceValue := []interface{}{(*services)[i], service.GetInt(stateCol), service.GetInt(checkedCol)}
if col.Name == "services_with_info" {
serviceValue = append(serviceValue, service.GetString(outputCol))
}
res = append(res, serviceValue)
}
return res
}
// VirtualColMembersWithState returns a list of hostgroup/servicegroup members with their states
func VirtualColMembersWithState(d *DataRow, col *Column) interface{} {
res := make([]interface{}, 0)
switch d.DataStore.Table.Name {
case TableHostgroups:
members := d.GetStringListByName("members")
hostsStore := d.DataStore.DataSet.tables[TableHosts]
stateCol := hostsStore.Table.GetColumn("state")
checkedCol := hostsStore.Table.GetColumn("has_been_checked")
for _, hostName := range *members {
host, ok := hostsStore.Index[hostName]
if !ok {
log.Errorf("Could not find host: %s\n", hostName)
continue
}
hostValue := []interface{}{hostName, host.GetInt(stateCol), host.GetInt(checkedCol)}
res = append(res, hostValue)
}
case TableServicegroups:
membersCol := d.DataStore.GetColumn("members")
members := d.GetServiceMemberList(membersCol)
servicesStore := d.DataStore.DataSet.tables[TableServices]
stateCol := servicesStore.Table.GetColumn("state")
checkedCol := servicesStore.Table.GetColumn("has_been_checked")
for i := range *members {
hostName := (*members)[i][0]
serviceDescription := (*members)[i][1]
service, ok := servicesStore.Index2[hostName][serviceDescription]
if !ok {
log.Errorf("Could not find service: %s - %s\n", hostName, serviceDescription)
continue
}
serviceValue := []interface{}{hostName, serviceDescription, service.GetInt(stateCol), service.GetInt(checkedCol)}
res = append(res, serviceValue)
}
}
return res
}
// VirtualColComments returns list of comment IDs
func VirtualColComments(d *DataRow, col *Column) interface{} {
comments, ok := d.DataStore.DataSet.cache.comments[d]
if ok {
return comments
}
return emptyInt64List
}
// VirtualColCommentsWithInfo returns list of comment IDs with additional information
func VirtualColCommentsWithInfo(d *DataRow, col *Column) interface{} {
commentsStore := d.DataStore.DataSet.tables[TableComments]
commentsTable := commentsStore.Table
authorCol := commentsTable.GetColumn("author")
commentCol := commentsTable.GetColumn("comment")
comments, ok := d.DataStore.DataSet.cache.comments[d]
if !ok {
return emptyInterfaceList
}
res := make([]interface{}, 0)
for i := range comments {
commentID := fmt.Sprintf("%d", comments[i])
comment, ok := commentsStore.Index[commentID]
if !ok {
log.Errorf("Could not find comment: %s\n", commentID)
continue
}
commentWithInfo := []interface{}{comments[i], comment.GetString(authorCol), comment.GetString(commentCol)}
res = append(res, commentWithInfo)
}
return res
}
// VirtualColDowntimes returns list of downtimes IDs
func VirtualColDowntimes(d *DataRow, col *Column) interface{} {
downtimes, ok := d.DataStore.DataSet.cache.downtimes[d]
if ok {
return downtimes
}
return emptyInt64List
}
// VirtualColDowntimesWithInfo returns list of downtimes IDs with additional information
func VirtualColDowntimesWithInfo(d *DataRow, col *Column) interface{} {
downtimesStore := d.DataStore.DataSet.tables[TableDowntimes]
downtimesTable := downtimesStore.Table
authorCol := downtimesTable.GetColumn("author")
commentCol := downtimesTable.GetColumn("comment")
downtimes, ok := d.DataStore.DataSet.cache.downtimes[d]
if !ok {
return emptyInterfaceList
}
res := make([]interface{}, 0)
for i := range downtimes {
downtimeID := fmt.Sprintf("%d", downtimes[i])
downtime, ok := downtimesStore.Index[downtimeID]
if !ok {
log.Errorf("Could not find downtime: %s\n", downtimeID)
continue
}
downtimeWithInfo := []interface{}{downtimes[i], downtime.GetString(authorCol), downtime.GetString(commentCol)}
res = append(res, downtimeWithInfo)
}
return res
}
// VirtualColCustomVariables returns a custom variables hash
func VirtualColCustomVariables(d *DataRow, col *Column) interface{} {
namesCol := d.DataStore.Table.GetColumn("custom_variable_names")
valuesCol := d.DataStore.Table.GetColumn("custom_variable_values")
names := d.GetStringList(namesCol)
values := d.GetStringList(valuesCol)
res := make(map[string]string, len(*names))
for i := range *names {
res[(*names)[i]] = (*values)[i]
}
return res
}
// VirtualColTotalServices returns 1 if there is long plugin output, 0 if not
func VirtualColTotalServices(d *DataRow, col *Column) interface{} {
val := d.GetIntByName("num_services")
return val
}
// getVirtualSubLMDValue returns status values for LMDSub backends
func (d *DataRow) getVirtualSubLMDValue(col *Column) (val interface{}, ok bool) {
ok = true
p := d.DataStore.Peer
peerData := p.StatusGet(SubPeerStatus).(map[string]interface{})
if peerData == nil {
return nil, false
}
switch col.Name {
case "status":
// return worst state of LMD and LMDSubs state
parentVal := p.StatusGet(PeerState).(PeerStatus)
if parentVal != PeerStatusUp {
val = parentVal
} else {
val, ok = peerData[col.Name]
}
case "last_error":
// return worst state of LMD and LMDSubs state
parentVal := p.StatusGet(LastError).(string)
val, ok = peerData[col.Name]
if parentVal != "" && (!ok || val.(string) == "") {
val = parentVal
}
default:
val, ok = peerData[col.Name]
}
return
}
// MatchFilter returns true if the given filter matches the given datarow.
func (d *DataRow) MatchFilter(filter *Filter) bool {
// recursive group filter
switch filter.GroupOperator {
case And:
for _, f := range filter.Filter {
if !d.MatchFilter(f) {
return false
}
}
return true
case Or:
for _, f := range filter.Filter {
if d.MatchFilter(f) {
return true
}
}
return false
}
// if this is a optional column and we do not meet the requirements, match against an empty default column
if filter.ColumnOptional != NoFlags && !d.DataStore.Peer.HasFlag(filter.Column.Optional) {
// duplicate filter, but use the empty column
f := &Filter{
Column: d.DataStore.Table.GetEmptyColumn(),
Operator: filter.Operator,
StrValue: filter.StrValue,
Regexp: filter.Regexp,
IsEmpty: filter.IsEmpty,
CustomTag: filter.CustomTag,
Negate: filter.Negate,
}
f.Column.DataType = filter.Column.DataType
if f.Negate {
return !f.Match(d)
}
return f.Match(d)
}
if filter.Negate {
return !filter.Match(d)
}
return filter.Match(d)
}
func (d *DataRow) getStatsKey(res *Response) string {
if len(res.Request.RequestColumns) == 0 {
return ""
}
keyValues := []string{}
for i := range res.Request.RequestColumns {
keyValues = append(keyValues, *(d.GetString(res.Request.RequestColumns[i])))
}
return strings.Join(keyValues, ListSepChar1)
}
// UpdateValues updates this datarow with new values
func (d *DataRow) UpdateValues(dataOffset int, data *[]interface{}, columns *ColumnList, timestamp int64) error {
if len(*columns) != len(*data)-dataOffset {
return fmt.Errorf("table %s update failed, data size mismatch, expected %d columns and got %d", d.DataStore.Table.Name.String(), len(*columns), len(*data))
}
for i := range *columns {
col := (*columns)[i]
i += dataOffset
switch col.DataType {
case StringCol:
d.dataString[col.Index] = *(interface2string((*data)[i]))
case StringListCol:
if col.FetchType == Static {
// deduplicate string lists
d.dataStringList[col.Index] = *d.deduplicateStringlist(interface2stringlist((*data)[i]))
} else {
d.dataStringList[col.Index] = *interface2stringlist((*data)[i])
}
case StringLargeCol:
d.dataStringLarge[col.Index] = *interface2stringlarge((*data)[i])
case IntCol:
d.dataInt[col.Index] = interface2int((*data)[i])
case Int64Col:
d.dataInt64[col.Index] = interface2int64((*data)[i])
case Int64ListCol:
d.dataInt64List[col.Index] = interface2int64list((*data)[i])
case FloatCol:
d.dataFloat[col.Index] = interface2float64((*data)[i])
case ServiceMemberListCol:
d.dataServiceMemberList[col.Index] = *interface2servicememberlist((*data)[i])
case InterfaceListCol:
d.dataInterfaceList[col.Index] = interface2interfacelist((*data)[i])
default:
log.Panicf("unsupported column %s (type %d) in table %s", col.Name, col.DataType, d.DataStore.Table.Name)
}
}
if timestamp == 0 {
timestamp = time.Now().Unix()
}
d.LastUpdate = timestamp
return nil
}
// UpdateValuesNumberOnly updates this datarow with new values but skips strings
func (d *DataRow) UpdateValuesNumberOnly(dataOffset int, data *[]interface{}, columns *ColumnList, timestamp int64) error {
if len(*columns) != len(*data)-dataOffset {
return fmt.Errorf("table %s update failed, data size mismatch, expected %d columns and got %d", d.DataStore.Table.Name.String(), len(*columns), len(*data))
}
for i := range *columns {
col := (*columns)[i]
i += dataOffset
switch col.DataType {
case IntCol:
d.dataInt[col.Index] = interface2int((*data)[i])
case Int64Col:
d.dataInt64[col.Index] = interface2int64((*data)[i])
case Int64ListCol:
d.dataInt64List[col.Index] = interface2int64list((*data)[i])
case FloatCol:
d.dataFloat[col.Index] = interface2float64((*data)[i])
case InterfaceListCol:
d.dataInterfaceList[col.Index] = interface2interfacelist((*data)[i])
}
}
d.LastUpdate = timestamp
return nil
}
// CheckChangedIntValues returns true if the given data results in an update
func (d *DataRow) CheckChangedIntValues(data *[]interface{}, columns *ColumnList) bool {
for j := range *columns {
col := (*columns)[j]
switch col.DataType {
case IntCol:
if interface2int((*data)[j]) != d.dataInt[(*columns)[j].Index] {
return true
}
case Int64Col:
if interface2int64((*data)[j]) != d.dataInt64[(*columns)[j].Index] {
return true
}
}
}
return false
}
func interface2float64(in interface{}) float64 {
switch v := in.(type) {
case float64:
return v
case int64:
return float64(v)
case int:
return float64(v)
case bool:
if v {
return 1
}
case string:
val, _ := strconv.ParseFloat(v, 64)
return val
default:
val, _ := strconv.ParseFloat(fmt.Sprintf("%v", v), 64)
return val
}
return 0
}
func interface2int(in interface{}) int {
switch v := in.(type) {
case float64:
return int(v)
case int64:
return int(v)
case int:
return v
case int32:
return int(v)
case bool:
if v {
return 1
}
return 0
case string:
val, _ := strconv.ParseInt(v, 10, 64)
return int(val)
}
val, _ := strconv.ParseInt(fmt.Sprintf("%v", in), 10, 64)
return int(val)
}
func interface2int64(in interface{}) int64 {
switch v := in.(type) {
case float64:
return int64(v)
case int64:
return v
case int:
return int64(v)
case bool:
if v {
return 1
}
case string:
val, _ := strconv.ParseInt(v, 10, 64)
return val
}
val, _ := strconv.ParseInt(fmt.Sprintf("%v", in), 10, 64)
return val
}
func interface2string(in interface{}) *string {
switch v := in.(type) {
case string:
dedupedstring := stringdedup.S(v)
return &dedupedstring
case *string:
return v
case nil:
val := ""
return &val
}
str := fmt.Sprintf("%v", in)
return &str
}
func interface2stringNoDedup(in interface{}) *string {
switch v := in.(type) {
case string:
return &v
case *string:
return v
case nil:
val := ""
return &val
}
str := fmt.Sprintf("%v", in)
return &str
}
func interface2stringlarge(in interface{}) *StringContainer {
switch v := in.(type) {
case string:
return NewStringContainer(&v)
case *string:
return NewStringContainer(v)
case nil:
val := ""
return NewStringContainer(&val)
case *StringContainer:
return v
}
str := fmt.Sprintf("%v", in)
return NewStringContainer(&str)
}
func interface2stringlist(in interface{}) *[]string {
switch list := in.(type) {
case *[]string:
return list
case []string:
return &list
case float64:
val := make([]string, 0, 1)
// icinga 2 sends a 0 for empty lists, ex.: modified_attributes_list
if in != 0 {
val = append(val, *(interface2string(in)))
}
return &val
case string, *string:
val := make([]string, 0, 1)
if in != "" {
val = append(val, *(interface2string(in)))
}
return &val
case []interface{}:
val := make([]string, 0, len(list))
for i := range list {
val = append(val, *(interface2string(list[i])))
}
return &val
}
log.Warnf("unsupported stringlist type: %#v (%T)", in, in)
val := make([]string, 0)
return &val
}
func interface2servicememberlist(in interface{}) *[]ServiceMember {
switch list := in.(type) {
case *[]ServiceMember:
return list
case []ServiceMember:
return &list
case []interface{}:
val := make([]ServiceMember, len(list))
for i := range list {
if l2, ok := list[i].([]interface{}); ok {
if len(l2) == 2 {
val[i][0] = *interface2string(l2[0])
val[i][1] = *interface2string(l2[1])
}
}
}
return &val
}
log.Warnf("unsupported servicelist type: %#v (%T)", in, in)
val := make([]ServiceMember, 0)
return &val
}
func interface2int64list(in interface{}) []int64 {
if list, ok := in.([]int64); ok {
return (list)
}
if in == nil {
return emptyInt64List
}
if list, ok := in.([]int); ok {
val := make([]int64, 0, len(list))
for i := range list {
val = append(val, interface2int64(list[i]))
}
return val
}
if list, ok := in.([]interface{}); ok {
val := make([]int64, 0, len(list))
for i := range list {
val = append(val, interface2int64(list[i]))
}
return val
}