download comon data file
[sfa.git] / cmdline / getNodes.py
1 #!/usr/bin/python
2 import sys
3 import os
4 import traceback
5 import urllib
6 from datetime import datetime
7 from optparse import OptionParser
8 from geni.util.rspec import Rspec
9
10 sfi_dir = os.path.expanduser("~/.sfi/")
11
12 def create_parser():
13     command = sys.argv[0]
14     argv = sys.argv[1:]
15     usage = "%(command)s [options]" % locals()
16     description = """getNodes will query comon and generate a list of nodes 
17 (plain or rspec) that meet the specified crieteria. If no criteria is 
18 specified, the default action is to return node comon considers 'alive' 
19 (resptime > 0)"""
20     options = ['alive']
21     cmp_options = ['rwfs', 'uptime', 'loads', 'meminfo', 'kernver', 'cpuspeed', 'txrate', 'rxrate', 'numslices', 'liveslices']
22      
23
24     parser = OptionParser(usage=usage,description=description)
25     for opt in options:
26         parser.add_option("--%s" % opt, dest="%s" % opt, action="store_true", 
27                           help = "available options [%s]" % ",".join(cmp_options))
28    
29     return parser    
30
31
32 def download_file(url, localFile):
33     webFile = urllib.urlopen(url)
34     localFile = open(localFile, 'w')
35     localFile.write(webFile.read())
36     localFile.close()    
37     
38
39 def get_comon_data():
40     date = datetime.now()
41     year = str(date.year)
42     month = str(date.month)
43     day = str(date.day)
44     if len(month) == 1:
45         month = "0" + month
46     if len(day) == 1:
47         day = "0" + day
48      
49     comon_data_filename = sfi_dir + os.sep + "comon_data.dat" 
50     comon_url = "http://comon.cs.princeton.edu/status/dump_comon_%s%s%s" % (year, month, day)
51     
52     print "storing comon data from %s in %s" % (comon_url, comon_data_filename)     
53     download_file(comon_url, comon_data_filename)
54
55     return comon_data_filename
56         
57 def main():
58     parser = create_parser()
59     comon_file = get_comon_data()
60     
61
62 if __name__ == '__main__':
63     main()