1+ """ Get Resources """
2+
3+ # pylint: disable=import-error
4+ from prismacloud .api import pc_api , pc_utility
5+ from tabulate import tabulate
6+
7+ import pandas as pd
8+ import time
9+ import datetime
10+ import string
11+ import random
12+ import os
13+
14+ # --Configuration-- #
15+
16+ parser = pc_utility .get_arg_parser ()
17+ parser .add_argument (
18+ 'week' ,
19+ type = int ,
20+ help = "number of week before today" )
21+ args = parser .parse_args ()
22+
23+ # --Initialize-- #
24+
25+ # pc_utility.prompt_for_verification_to_continue(args)
26+ settings = pc_utility .get_settings (args )
27+ pc_api .configure (settings )
28+
29+ dt = datetime .datetime (year = 2022 , month = 1 , day = 1 )
30+ data = ['critical' , 'high' , 'medium' , 'low' , 'information' ]
31+ df_trend = pd .DataFrame (data , columns = ['Policy Severity' ])
32+ start_ts = time .mktime (dt .timetuple ())* 1000
33+
34+ # initializing size of string
35+ N = 5
36+
37+ # using random.choices()
38+ # generating random strings
39+ res = '' .join (random .choices (string .ascii_uppercase + string .digits , k = N ))
40+
41+ for x in range (args .week ):
42+ end_ts = time .mktime ((datetime .datetime .today () - datetime .timedelta (weeks = x )).timetuple ())* 1000
43+ print ('API - Gernerate new CSV Report ...' , end = '' )
44+ body_params = {
45+ "detailed" : True ,
46+ "fields" :[
47+ "alert.id" ,
48+ "alert.status" ,
49+ "alert.time" ,
50+ "cloud.account" ,
51+ "cloud.accountId" ,
52+ "cloud.region" ,
53+ "resource.id" ,
54+ "resource.name" ,
55+ "policy.name" ,
56+ "policy.type" ,
57+ "policy.severity"
58+ ],
59+ "filters" :[
60+ {"name" :"policy.severity" , "operator" :"=" , "value" : "high" },
61+ {"name" :"policy.severity" , "operator" :"=" , "value" : "critical" },
62+ {"name" :"policy.severity" , "operator" :"=" , "value" : "medium" },
63+ {"name" :"policy.severity" , "operator" :"=" , "value" : "low" },
64+ {"name" :"alert.status" ,"operator" :"=" , "value" : "open" }
65+ ],
66+ "groupBy" : [
67+ "cloud.account"
68+ ],
69+ "limit" : 2000 ,
70+ "offset" : 0 ,
71+ "sortBy" : [
72+ "cloud.account"
73+ ],
74+ "timeRange" : {
75+ "type" : "absolute" ,
76+ "value" : {
77+ "startTime" : start_ts ,
78+ "endTime" : end_ts
79+ }
80+ }
81+ }
82+
83+ print ()
84+ print ('Creating the Alert Report...' , end = '' )
85+ print ()
86+ alert_report = pc_api .alert_csv_create (body_params )
87+ print ('Report Created with Report ID: %s' % alert_report ['id' ])
88+ report_time = time .strftime ("%Y%m%d" )
89+ report_filename = "./customer-report-" + report_time + "-" + res + "-" + str (x ) + ".csv"
90+ column_name = str (x ) + ' Week ago'
91+ print ()
92+
93+ report_ready = False
94+ report_dir = '.'
95+
96+ while (not report_ready ):
97+ alert_report_update = pc_api .alert_csv_status (alert_report ['id' ])
98+ # print('Getting the Alert Report Status...', alert_report_update['status'])
99+ time .sleep (2.5 )
100+ if (alert_report_update ['status' ] == 'READY_TO_DOWNLOAD' ):
101+ csv_report = pc_api .alert_csv_download (alert_report ['id' ])
102+ # Write Download Report File to Current Report Directory
103+ file = open (report_filename , "w" )
104+ file .write (csv_report )
105+ file .close ()
106+ # print("Alert Report Downloaded...")
107+ break
108+
109+ df = pd .read_csv (report_filename , usecols = ['Policy Severity' ])
110+ df_severity = df .groupby (['Policy Severity' ])['Policy Severity' ].count ().to_frame ()
111+ df_severity .columns = [column_name ]
112+ df_severity = df_severity .reset_index ()
113+ # df_trend = df_trend.merge(df_severity,left_on='Policy Severity',right_on='Policy Severity')
114+ df_trend = df_trend .merge (df_severity , on = 'Policy Severity' , how = 'left' )
115+ df_trend [column_name ].fillna (0 , inplace = True )
116+ os .remove (report_filename )
117+
118+ df_trend = df_trend .set_index ('Policy Severity' ).transpose ()
119+ print (tabulate (df_trend , headers = 'keys' , tablefmt = 'psql' ))
0 commit comments