-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding graph_csv.py script #5
base: master
Are you sure you want to change the base?
Changes from all commits
0ba784b
6d10297
52fe439
de5b2c4
1bb4df6
6fcdffd
7a35aac
fa76dca
643a946
2c71dc6
c374dd8
ef5d7cf
104acd4
390b1db
13f31d5
5b34661
b46493f
4475c16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#! /usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
Copyright (C) 2017 Unicamp-OpenPower | ||
Licensed under the Apache License, Version 2.0 (the “License”); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an “AS IS” BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
""" | ||
import argparse | ||
import thread | ||
import time | ||
import os | ||
|
||
INTERVAL = 10 | ||
PYTHON_VERSION = 'python2.7' | ||
|
||
def build_commands(args): | ||
""" | ||
function used to build the ipmi and csv commands | ||
""" | ||
year = time.strftime("%Y") | ||
month = int(time.strftime("%m")) | ||
day = int(time.strftime("%d")) | ||
date=str(year)+str(month)+str(day) | ||
|
||
csv_command = PYTHON_VERSION + ' csvcreator.py --name=last --date=' + date + \ | ||
' --jsonfile=' | ||
csv_command = csv_command + args.jsonfile | ||
|
||
if args.csv_interval: | ||
global CSV_INTERVAL | ||
CSV_INTERVAL = args.csv_interval | ||
else: | ||
CSV_INTERVAL = 300 | ||
|
||
powergraph_command = PYTHON_VERSION + ' powergraph.py' | ||
if not args.host: | ||
print "\nERROR: hostname is required.\n" | ||
parser.print_help() | ||
sys.exit(1) | ||
else: | ||
powergraph_command += ' --host=' + args.host | ||
if args.port: | ||
powergraph_command += ' --port=' + args.port | ||
if not args.user: | ||
print "\nERROR: username is required.\n" | ||
parser.print_help() | ||
sys.exit(1) | ||
else: | ||
powergraph_command += ' --user=' + args.user | ||
if args.passwd: | ||
powergraph_command += ' --passwd=' + args.passwd | ||
if args.interval: | ||
powergraph_command += ' --interval=' + args.interval | ||
else: | ||
powergraph_command += ' --interval=1' | ||
powergraph_command += ' --store' | ||
|
||
return powergraph_command,csv_command | ||
|
||
def get_input(): | ||
""" | ||
function to get the arguments from the user | ||
""" | ||
parser = argparse.ArgumentParser(description='Parameters') | ||
|
||
parser.add_argument('--host', | ||
help='adress of the host', required=True) | ||
parser.add_argument('--port', | ||
help='port of IPMI host', required=True) | ||
parser.add_argument('--user', | ||
help='user allowed to acces IPMI', required=True) | ||
parser.add_argument('--passwd', | ||
help='password for the specific user', required=True) | ||
parser.add_argument('--interval', | ||
help='seconds between each data reading') | ||
parser.add_argument('--nread', | ||
help='number of time to collect data') | ||
|
||
parser.add_argument('--jsonfile', | ||
help='jsonfile to be converted as csv', required=True) | ||
parser.add_argument('--csv_interval', | ||
help='interval you want to create a new csv file') | ||
parser.add_argument('--tail_length', | ||
help='the amount of inputs do get from the csv file ' | ||
'in order to create the input for the graphic ' | ||
'visualization', | ||
default=300) | ||
|
||
return parser.parse_args() | ||
|
||
def run_collector(command): | ||
""" | ||
function to run the collection of data | ||
""" | ||
try: | ||
os.system(command) | ||
except OSError as err: | ||
print ("OS erros: {0}".format(err)) | ||
|
||
|
||
def run_csv(command,tail_length): | ||
""" | ||
function to run the csv generator | ||
""" | ||
while True: | ||
time.sleep(float(CSV_INTERVAL)) | ||
try: | ||
os.system(command) | ||
os.system("tail -n 300 last.csv > aux.csv") | ||
os.system("mv -f aux.csv last.csv") | ||
except OSError as err: | ||
print("OS error: {0}".format(err)) | ||
|
||
def main(): | ||
""" | ||
Main execution. | ||
""" | ||
args = get_input() | ||
powergraph_command, csv_command = build_commands(args) | ||
thread.start_new_thread(run_collector, (powergraph_command,)) | ||
thread.start_new_thread(run_csv, (csv_command, args.tail_length, )) | ||
try: | ||
while True: | ||
pass | ||
except (KeyboardInterrupt, SystemExit): | ||
print "\nExecution cancelled. Bye!" | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
""" | ||
Invoking the main execution function. | ||
""" | ||
main() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/bin/bash | ||
|
||
#script made to make sure the ipmi data collector is running. | ||
#used alongside with crontab | ||
|
||
#name of the process that can be running to get data | ||
threads=('powergraph.py' 'graph_csv.py') | ||
|
||
#get size of the threads array | ||
size=$(echo ${#threads[@]}) | ||
size=$(echo "$size-1" | bc) | ||
|
||
for i in $(seq 0 $size); do | ||
#if the number of procees running are only one, so its the grep itself | ||
#and nedd to be restarted | ||
ps_size=$(ps aux | grep ${threads[i]} | wc -l) | ||
if [ "$ps_size" == 1 ]; then | ||
for i in $(seq 0 $size); do | ||
for pid in $(pgrep -f ${threads[i]}); do | ||
kill $pid | ||
done | ||
done | ||
#comando to launch the data getter again | ||
python2.7 graph_csv.py --host='<YOUR_HOST>' --port='<YOUR_PORT>' \ | ||
--user='<YOUR_USER>' --passwd='<YOUR_PASS'> \ | ||
--interval=1 --jsonfile=powerdata.json & | ||
fi | ||
done |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ | |
NREAD = 10 | ||
INFINITY = False | ||
STORE = False | ||
|
||
FEEDBACK = False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to split the patch in two: one for the run-all-patch and another for this one. Also, ensure you run pylint on all files. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry. but I didn't undestand waht you mean. |
||
|
||
def savedb(input): | ||
""" | ||
|
@@ -97,6 +97,9 @@ def get_input(): | |
parser.add_argument('--nread', help='number of time to collect data') | ||
parser.add_argument('--store', action='store_true', | ||
help='save the data collected in a nosql db') | ||
parser.add_argument('--feedback', action='store_true', | ||
help='print the collecting status') | ||
|
||
args = parser.parse_args() | ||
return args, parser | ||
|
||
|
@@ -135,6 +138,9 @@ def build_command(args, parser): | |
if args.store: | ||
global STORE | ||
STORE = True | ||
if args.feedback: | ||
global FEEDBACK | ||
FEEDBACK = True | ||
return cmd | ||
|
||
|
||
|
@@ -161,7 +167,7 @@ def run(command, counter): | |
infos['Year'] = aux[2][2:6] | ||
infos['Energy'] = energy | ||
info = create_string(counter + 1, infos) | ||
if not STORE: | ||
if not STORE or FEEDBACK: | ||
print info | ||
else: | ||
savedb(info.split('|')) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can remove python 2.7 from the command specification. Given we added it at the top of file. In addition, ensure you chmod +x you Python script before pushing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!