-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
95 lines (76 loc) · 3.29 KB
/
main.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
from datetime import datetime, timedelta, date
import argparse
import dateutil.parser # pip install python-dateutil
from dateparser import dateparser
# from branch import branch_daily, branch_hourly
from DatadogMetric import get_metric,DatadogDetail
from json2csv import list_from_json,write_csv
yesterday = datetime.now() - timedelta(hours=2)
# yesterday = yesterday.strftime('%Y-%m-%d')
yesterday = yesterday.strftime('%Y-%m-%d %H:00:00')
today = datetime.now() - timedelta(hours=1)
# today = datetime.now() - timedelta(hours=1)
today = today.strftime('%Y-%m-%d %H:00:00')
# today = today.strftime('%Y-%m-%d')
kpi="Disk_Used"
interval="hourly"
parser = argparse.ArgumentParser()
parser.add_argument('-s', "--startdate",help="The Start Date - format YYYY-MM-DD",default=yesterday,required=False,type=dateutil.parser.isoparse)
parser.add_argument('-e', "--enddate",help="The End Date format YYYY-MM-DD (Inclusive)",default=today,required=False,type=dateutil.parser.isoparse)
parser.add_argument('-k', "--kpi",help="KPI metric ex: Disk_Used",default=kpi,required=False)
parser.add_argument('-i', "--interval",help="Interval Data, Daily / Hourly ",default=interval,required=False)
args = parser.parse_args()
argstartdate=args.startdate
argenddate=args.enddate
argkpi=args.kpi
if __name__ == '__main__':
### Get Start and End Date and Datadog from Argument, if not given, use default value ###
start_date=argstartdate
end_date=argenddate
# print(start_date)
# print(end_date)
str_start_date = datetime.strftime(start_date, '%Y-%m-%d %H:00:00')
start_date = datetime.strptime(str_start_date, '%Y-%m-%d %H:00:00')
str_end_date = datetime.strftime(end_date, '%Y-%m-%d %H:00:00')
end_date = datetime.strptime(str_end_date, '%Y-%m-%d %H:00:00')
# Convert Date to datetime and epoch
# date=dateparser.gethour(start_date,end_date)
# ystart=date[0]
# ystarttime=date[1]
# yend=date[2]
# yendtime=date[3]
#
# print(ystarttime)
# print(yendtime)
# ### Get Datadog KPI you want to extract from Argument, if not given, use default value "Disk_Used" ###
# kpi = argkpi
# kpi = "Nginx_Request"
# # KPI Metric
# metric = DatadogDetail.DDdatabase(kpi)[0]
# # Group by Column
# column = DatadogDetail.DDdatabase(kpi)[1]
# # Rollup Method
# rollup = DatadogDetail.DDdatabase(kpi)[2]
#
# ### Specified your group by or column ###
# list_column = list(column.split(","))
# # print(list_column)
# ln_list_column = len(list_column)
# ### Specified your group by or column ###
#
# # Get Metric through API #
# # interval = "daily"
# if interval == "daily":
# # Daily Metric
# get_metric.get_metric(ystart, yend, yesterday, column, metric, interval)
# # Convert Json Result to CSV #
# jsonfile = metric + interval + yesterday + ".json"
# else:
# # Hourly Metric
# get_metric.get_metric(ystart, yend, yesterday, column, metric,interval)
# jsonfile = metric + interval + yesterday + ".json"
#
# data_list=list_from_json.create_list_from_json(jsonfile,list_column,ln_list_column)
# # pathfile='/home/work/result/datadog/'
# pathfile='.'
# csv=write_csv.write_csv(yesterday,data_list,pathfile,metric,list_column)