-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfetchalldatatypes.py
147 lines (131 loc) · 5.17 KB
/
fetchalldatatypes.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
import argparse
import os
import sys
import library.localstore as store
import library.migrationlogger as m_logger
import library.utils as utils
import library.clients.insightsclient as insightsclient
"""Get Data being reported from a host
Query Metrics being reported
Query Events being reported
"""
logger = m_logger.get_logger(os.path.basename(__file__))
def configure_parser(is_standalone: bool = True):
parser = argparse.ArgumentParser(
description='Get Metrics and Events reported from a host'
)
parser.add_argument(
'--hostsFile',
'--hosts_file',
nargs=1,
type=str,
required=False,
help='Path to file with host names',
dest='hosts_file'
)
parser.add_argument(
'--sourceAccount',
'--source_account_id',
nargs=1,
type=int,
required=is_standalone,
help='Source accountId',
dest='source_account_id'
)
parser.add_argument(
'--sourceApiKey',
'--source_api_key',
nargs=1,
type=str,
required=False,
help='Source account API Key or set environment variable ENV_SOURCE_API_KEY',
dest='source_api_key'
)
parser.add_argument(
'--insightsQueryKey',
'--insights_query_key',
nargs=1,
type=str,
required=False,
help='Insights Query Key or set environment variable ENV_INSIGHTS_QUERY_KEY',
dest='insights_query_key'
)
parser.add_argument(
'--region',
nargs=1,
type=str,
required=False,
default='us',
help='NR Region us | eu (default : us)',
dest='region'
)
return parser
def fetch_all_event_types(query_key: str,acct_id : int, region: str):
show_events_query = "SHOW EVENT TYPES"
response = insightsclient.execute(query_key, acct_id, show_events_query, region)
if 'error' in response:
logger.error('Could not fetch event types')
logger.error(response['error'])
return []
else:
logger.info(response)
return response['json']['results'][0]['eventTypes']
def fetch_event_type_count(host_name: str, event_type: str, query_key: str, acct_id: int, region: str):
event_count_query_template = "FROM %(eventType)s SELECT COUNT(*) WHERE entityName = '%(host)s' SINCE 1 WEEK AGO"
event_count_query = event_count_query_template % {'eventType': event_type, 'host': host_name}
response = insightsclient.execute(query_key, acct_id, event_count_query, region)
if 'error' in response:
logger.error('Error executing query ' + event_count_query)
logger.error('Error fetching event count ' + response)
return 0
else:
return response['json']['results'][0]['count']
def fetch_metrics(host_name: str, query_key: str, acct_id: int, region: str):
logger.info("fetching metrics for " + host_name)
fetch_metrics_query = "FROM Metric SELECT uniques(metricName) " \
"WHERE entityName = '%s' " \
"SINCE 1 week ago LIMIT MAX" % host_name
response = insightsclient.execute(query_key, acct_id, fetch_metrics_query, region)
if 'error' in response:
logger.error('Could not fetch metrics for %s', host_name)
logger.error(response['error'])
return []
else:
return response['json']['results'][0]['members']
def fetch_data_types(host_file_path: str, acct_id: int, api_key: str, query_key: str, region='us'):
host_names = store.load_names(host_file_path)
all_event_types = fetch_all_event_types(query_key, acct_id, region)
for host_name in host_names:
host_data = [['entityName', 'dataType', 'metricOrEventName']]
logger.info('fetching data types for ' + host_name)
dim_metrics = fetch_metrics(host_name, query_key, acct_id, region)
logger.info("Fetched metrics %d", len(dim_metrics))
for dim_metric in dim_metrics:
host_data.append([host_name, 'Metric', dim_metric])
for event_type in all_event_types:
if event_type != 'Metric':
event_count = fetch_event_type_count(host_name, event_type, query_key, acct_id, region)
if event_count > 0:
host_data.append([host_name, 'Event', event_type])
logger.info("Total event and metrics found %d", len(host_data) - 1)
if len(host_data) > 1:
store.save_host_data_csv(host_name, host_data)
else:
logger.info('No metrics or events found for ' + host_name)
def main():
parser = configure_parser()
args = parser.parse_args()
api_key = utils.ensure_source_api_key(args)
if not api_key:
utils.error_and_exit('api_key', 'ENV_SOURCE_API_KEY')
insights_query_key = utils.ensure_insights_query_key(args)
if not insights_query_key:
utils.error_and_exit('query_api_key', 'ENV_QUERY_API_KEY')
region = utils.ensure_region(args)
hosts_file = args.hosts_file[0] if args.hosts_file else None
if not hosts_file:
logger.error('host file must be specified.')
sys.exit()
fetch_data_types(hosts_file, args.source_account_id[0], api_key, insights_query_key, region)
if __name__ == '__main__':
main()