Several fixes to configuration, dependencies, and support tools.
[monitor.git] / tools / automate / fetch.py
1 #!/usr/bin/python
2
3 import csv
4 import sys
5 import os
6 from glob import glob
7
8 import vxargs
9 from monitor import parser as parsermodule
10 from monitor import common
11 from automate import *
12
13 def build_vx_args_external(shell_cmd):
14     args = shell_cmd.split()
15     return args
16
17 def vx_start_external(filelist,outdir,cmd, timeout=0, threadcount=20):
18     args = build_vx_args_external(cmd)
19     vxargs.start(None, threadcount, filelist, outdir, False, args, timeout)
20
21 def build_vx_args(shell_cmd):
22     ssh_options="-q -o UserKnownHostsFile=junkssh -o StrictHostKeyChecking=no"
23     cmd="""ssh %s root@{} """  % ssh_options
24     args = cmd.split()
25     args.append(shell_cmd)
26     return args
27
28 def vx_start(filelist,outdir,cmd, timeout=0):
29     args = build_vx_args(cmd)
30     vxargs.start(None, 20, filelist, outdir, False, args, timeout)
31
32 def get_hostlist_from_myops(filter):
33     ret = []
34     curl_url = "curl -s 'http://myops.planet-lab.org:5984/query"
35     curl_params = "fields=hostname&skip_header&filter=%s'" % filter
36     curl_cmd = "%s?%s" % (curl_url, curl_params)
37     print curl_cmd
38     f = os.popen(curl_cmd, 'r')
39     for h in f.read().split():
40         ret.append((h, ''))
41     return ret
42
43 if __name__ == "__main__":
44
45     parser = parsermodule.getParser(['nodesets'])
46     parser.set_defaults(outdir=None,
47                         timeout=0,
48                         simple=False,
49                         threadcount=20,
50                         external=False,
51                         myopsfilter=None,
52                         run=False,
53                         template=None,
54                         cmdfile=None,)
55
56     parser.add_option("", "--timeout", dest="timeout", metavar="seconds",
57                         help="Number of seconds to wait before timing out on host.")
58     parser.add_option("", "--myopsfilter", dest="myopsfilter", metavar="",
59                         help="filter string to pass directly to myops query")
60     parser.add_option("", "--outdir", dest="outdir", metavar="dirname",
61                         help="Name of directory to place output")
62     parser.add_option("", "--cmd", dest="cmdfile", metavar="filename",
63                         help="Name of file that contains a unix-to-csv command " + \
64                              "to run on the hosts.")
65     parser.add_option("", "--external", dest="external",  action="store_true",
66                         help="Run commands external to the server. The default is internal.")
67     parser.add_option("", "--template", dest="template", 
68                         help="Command template for external commands; substitutes {} with hostname.")
69     parser.add_option("", "--threadcount", dest="threadcount", 
70                         help="Number of simultaneous threads: default 20.")
71
72     config = parsermodule.parse_args(parser)
73
74     if config.outdir == None: 
75         outdir="checkhosts"
76     else: 
77         outdir=config.outdir
78
79     if not os.path.exists(outdir):
80         os.system('mkdir -p %s' % outdir)
81
82     #if config.site is not None or \
83     #   config.nodeselect is not None or \
84     #   config.nodegroup is not None:
85     #    print "TODO: implement support for nodeselect and site queries."
86     #    print "%s %s %s" % (config.site, config.nodeselect, config.nodegroup)
87     #    sys.exit(1)
88     if config.myopsfilter is not None:
89         filelist = get_hostlist_from_myops(config.myopsfilter)
90     else:
91         nodelist = common.get_nodeset(config)
92         if len(nodelist) > 0:
93             filelist = [ (x, '') for x in nodelist ]
94         elif os.path.exists(str(config.nodelist)) and os.path.isfile(config.nodelist):
95             filelist = vxargs.getListFromFile(open(config.nodelist,'r'))
96         elif os.path.exists(str(config.nodelist)) and os.path.isdir(config.nodelist):
97             filelist = get_hostlist_from_dir(config.nodelist)
98         elif config.node is not None:
99             filelist = [(config.node, '')]
100         else:
101             # probably no such file.
102             raise Exception("No such file %s" % config.nodelist)
103
104     if config.cmdfile == None and config.template == None:
105         f = open("command.txt",'r')
106         cmd = f.read()
107     elif config.template is not None and config.external:
108         cmd = config.template
109     else:
110         f = open(config.cmdfile,'r')
111         cmd = f.read()
112
113     print filelist
114
115     if config.external or config.template is not None:
116         vx_start_external(filelist, outdir, cmd, int(config.timeout), int(config.threadcount))
117     else:
118         vx_start(filelist, outdir, cmd, int(config.timeout))
119