-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensorGroup.py
executable file
·180 lines (139 loc) · 7.82 KB
/
sensorGroup.py
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
#!/usr/bin/env python
#
import sys
from cbapi.response.models import SensorGroup, Site, Process
from cbapi.example_helpers import build_cli_parser, get_cb_response_object
from collections import defaultdict
from datetime import datetime, timezone, timedelta
import logging
#logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.WARNING)
log = logging.getLogger(__name__)
delta = timedelta(days=21)
start_date = datetime.now(timezone.utc)-delta
def list_sensor_groups(cb, parser, args):
if args.group_name:
selected_groups = args.group_name.split(",")
log.info("Getting Data for: {}".format(selected_groups))
totals = defaultdict(dict)
sensor_stats = defaultdict(dict)
print("Sensor Group,Sensor Group Backlog Total,Sensor Group Sensor Count,Sensors with 0 Processes")
for sg in selected_groups:
log.info("Looking at Sensor Group: {}".format(sg))
g = cb.select(SensorGroup).where("name:{0}".format(sg)).first()
log.info("Sensor Group {} has {} sensors".format(sg, len(g.sensors)))
if g is None:
log.info("Sensor Group: {} is not valid".format(sg))
else:
grouptotal = 0
sensortotal = 0
noproctotal = 0
for sensor in g.sensors:
if args.check_online:
if sensor.status == "Online":
log.info("Sensor {} is online".format(sensor.hostname))
if args.check_proc:
log.info("Checking for zero processes")
query = cb.select(Process).where("hostname:{0}".format(sensor.hostname)).first()
if query is None:
noproctotal += 1
totals[g.name][sensor.hostname] = "noproc"
sensor_stats[sensor.hostname]["noproc"] = "yes"
sensortotal += 1
grouptotal += int(sensor.num_eventlog_bytes)
sensor_stats[sensor.hostname]["backlog"] = int(sensor.num_eventlog_bytes)
if args.check_date:
if sensor.last_checkin_time >= start_date:
if args.check_proc:
log.info("Checking for zero processes")
query = cb.select(Process).where("hostname:{0}".format(sensor.hostname)).first()
if query is None:
noproctotal += 1
totals[g.name][sensor.hostname] = "noproc"
sensor_stats[sensor.hostname]["noproc"] = "yes"
sensortotal += 1
grouptotal += int(sensor.num_eventlog_bytes)
sensor_stats[sensor.hostname]["backlog"] = int(sensor.num_eventlog_bytes)
sensor_stats[sensor.hostname]["last_checkin"] = sensor.last_checkin_time.strftime("%Y-%m-%d")
sensor_stats[sensor.hostname]["sensor_group"] = sensor.group.name
sensor_stats[sensor.hostname]["dns_name"] = sensor.dns_name
sensor_stats[sensor.hostname]["os"] = sensor.os
sensor_stats[sensor.hostname]["network_ifs"] = sensor.network_interfaces
print("{},{},{},{}".format(g.name, grouptotal, sensortotal, noproctotal))
totals[g.name]["backlog"] = grouptotal
totals[g.name]["count"] = sensortotal
totals[g.name]["noproc"] = noproctotal
# for key in totals[g.name]:
# if key != "backlog":
# if key != "count":
# if key!= "noproc":
# print(key)
print("Hostname,Sensor Group,DNS Name,OS,Network Interfaces,Backlog(Bytes),Backlog(MiB),Last Checkin")
for key, value in sensor_stats.items():
if "noproc" in value:
print(key+","+str(value["backlog"])+","+value["noproc"])
print("{},{},{},\"{}\",\"{}\",{},{},{}".format(key,value["sensor_group"],value["dns_name"],value["os"],value["network_ifs"],value["backlog"],int(value["backlog"])/1024/1024,value["last_checkin"]))
print(totals)
print(len(sensor_stats))
else:
log.info("Getting Data for: All Sensor Groups")
totals = defaultdict(dict)
print("Sensor Group,Sensor Group Backlog Total,Sensor Group Sensor Count,Sensors with 0 Processes")
for g in cb.select(SensorGroup):
log.info("Looking at Sensor Group: {}".format(g.name))
grouptotal = 0
sensortotal = 0
noproctotal = 0
for sensor in g.sensors:
if sensor.status == "Online":
log.info("Sensor {} is online".format(sensor.hostname))
if args.check_proc:
log.info("Checking for zero processes")
query = cb.select(Process).where("hostname:{0}".format(sensor.hostname)).first()
if query is None:
log.info("Sensor {} has 0 processes!".format(sensor.hostname))
noproctotal += 1
totals[g.name][sensor.hostname] = "noproc"
sensortotal += 1
grouptotal += int(sensor.num_eventlog_bytes)
print("{},{},{},{}".format(g.name,grouptotal,sensortotal,noproctotal))
totals[g.name]["backlog"] = grouptotal
totals[g.name]["count"] = sensortotal
totals[g.name]["noproc"] = noproctotal
# for key in totals[g.name]:
# if key != "backlog":
# if key != "count":
# print(key)
print(totals)
def list_sensors(cb, parser, args):
if args.group_name:
group = cb.select(SensorGroup).where("name:{0}".format(args.group_name)).first()
else:
group = min(cb.select(SensorGroup), key=lambda x: x.id)
for sensor in group.sensors:
if sensor.status == "Online":
query = cb.select(Process).where("hostname:{0}".format(sensor.hostname))
print(sensor.hostname+","+str(len(query)))
def main():
parser = build_cli_parser()
commands = parser.add_subparsers(help="Sensor Group commands", dest="command_name")
list_command = commands.add_parser("list", help="Check all or selected configured sensor groups")
list_command.add_argument("-n", "--name", action="store", help="Sensor group name", required=False,
dest="group_name")
list_command.add_argument("-p", "--proc", action="store_true", help="Check for zero process hosts", required=False,
dest="check_proc")
list_command.add_argument("-o", "--online", action="store_true", help="Only evaluate online hosts", required=False,
dest="check_online")
list_command.add_argument("-d", "--date", action="store_true",
help="Evaluate hosts online within the past 21 days", required=False, dest="check_date")
list_sensors_command = commands.add_parser("list-sensors", help="List all configured sensor groups")
list_sensors_command.add_argument("-n", "--name", action="store", help="Sensor group name", required=False,
dest="group_name")
args = parser.parse_args()
cb = get_cb_response_object(args)
if args.command_name == "list":
return list_sensor_groups(cb, parser, args)
if args.command_name == "list-sensors":
return list_sensors(cb, parser, args)
if __name__ == "__main__":
sys.exit(main())