|
7 | 7 | # or put it to config file;
|
8 | 8 | #==========================================================#
|
9 | 9 | import os
|
| 10 | +import ConfigParser |
10 | 11 | import re
|
11 |
| -from datetime import datetime |
12 | 12 | import fileinput
|
13 | 13 | import xlsxwriter
|
14 | 14 |
|
15 |
| -# HARD-CODED!!! |
16 |
| -sar_dir = '/var/log/sa' |
17 |
| -location = 'Seattle' |
18 |
| -# hostname = os.getenv('HOSTNAME') |
19 |
| -# Command above does not work from cron. It return 'None'. |
20 |
| -# But it works from command line. |
21 |
| -# It looks like os.getenv require some environment settings. |
| 15 | +# Open config file and read parameters |
| 16 | +# __file__ is a script name from command line. |
| 17 | +# Example: ./script.py or /full/path/to/the/script.py |
| 18 | +# os.path.splitext(__file__)[0] return __file__ without extention (.py); |
| 19 | +# .split('/')[-1] return string after last '/' |
| 20 | +script_name = os.path.splitext(__file__)[0].split('/')[-1] |
| 21 | +config_param = ConfigParser.RawConfigParser() |
| 22 | +config_param.read('./config/' + script_name + '.conf') |
22 | 23 |
|
23 | 24 | # Generate output file name
|
24 |
| -timestamp = datetime.now().strftime("%Y%m%d") |
25 |
| -hostname = os.uname()[1] |
26 |
| -output_dir = '/home/oracle/scripts/Excel/' |
27 |
| -excel_file = output_dir + 'sar_mem4excel_' + location + '_' + hostname + '_' + timestamp + '.xlsx' |
28 |
| -column_headers = ('Date' |
29 |
| - ,'Free Memory' |
30 |
| - ,'Used Memore' |
31 |
| - ,'Buffers' |
32 |
| - ,'Cached' |
33 |
| - ,'Free Swap' |
34 |
| - ,'Used Swap' |
35 |
| - ,'Swap Cad') |
36 |
| -workbook = xlsxwriter.Workbook(excel_file) |
37 |
| -worksheet_name = 'Memory Usage' |
38 |
| -worksheet = workbook.add_worksheet(worksheet_name) |
39 |
| -bold = workbook.add_format({'bold': 1}) |
40 |
| -file_timestamp = '' |
41 |
| -column_headers_done = 0 |
42 |
| -excel_data = [] |
43 |
| -row_number = 0 |
| 25 | +location = os.environ['GE0_LOCATION'] |
| 26 | +hostname = os.uname()[1] |
| 27 | +excel_file = config_param.get('MEMORY', 'output_dir') + script_name + '_' + location + '_'+hostname + '_' + os.environ['THE_TIME'] + '.xlsx' |
44 | 28 |
|
45 |
| -read_following_lines = 0 |
| 29 | +# Setup spreadsheet |
| 30 | +file_timestamp = '' |
| 31 | +worksheet_name = 'Memory Usage' |
| 32 | +workbook = xlsxwriter.Workbook(excel_file) |
| 33 | +worksheet = workbook.add_worksheet(worksheet_name) |
| 34 | +bold = workbook.add_format({'bold': 1}) |
| 35 | +column_headers = 0 |
| 36 | +row_number = 0 |
| 37 | +start_reading = 0 |
| 38 | +excel_data = [] |
46 | 39 |
|
47 | 40 | #-------------------------------------------#
|
48 | 41 | # Read SAR log files in chronological order #
|
49 | 42 | #-------------------------------------------#
|
50 | 43 | # go to SAR directory
|
51 |
| -os.chdir(sar_dir) |
| 44 | +os.chdir(config_param.get('MEMORY', 'sar_dir')) |
52 | 45 |
|
53 | 46 | # List all files in this directory in chrono order
|
54 | 47 | all_sar_files = sorted(filter(os.path.isfile, os.listdir('.')), key=os.path.getmtime)
|
|
74 | 67 | # Start record the data
|
75 | 68 | if re.match('.*\skbmemfree\s.*', the_line):
|
76 | 69 | # Set the flag
|
77 |
| - read_following_lines = 1 |
| 70 | + start_reading = 1 |
78 | 71 | # Populate header once only
|
79 |
| - if column_headers_done == 0: |
80 |
| - worksheet.write_row('A1', column_headers, bold) |
81 |
| - column_headers_done = 1 |
82 |
| - #print column_headers |
| 72 | + if column_headers == 0: |
| 73 | + worksheet.write_row('A1', config_param.get('MEMORY','column_headers').split(','), bold) |
| 74 | + column_headers = 1 |
83 | 75 | continue
|
84 | 76 |
|
85 | 77 | # Stop record the data
|
86 |
| - if re.match('^Average.*', the_line) and read_following_lines == 1: |
87 |
| - read_following_lines = 0 |
| 78 | + if re.match('^Average.*', the_line) and start_reading == 1: |
| 79 | + start_reading = 0 |
88 | 80 | continue
|
89 | 81 |
|
90 | 82 | # Record the data
|
91 |
| - if read_following_lines == 1: |
| 83 | + if start_reading == 1: |
92 | 84 | # Skip all next headers
|
93 | 85 | if re.match('.*\skbmemfree\s.*', the_line):
|
94 | 86 | continue
|
|
0 commit comments