datagram_client.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 # The basis for this UDP (User Datagram Protocol) portion of this code
3 # came from: http://en.wikipedia.org/wiki/User_Datagram_Protocol (2008-05-01)
4 # This script is a copy of the one used in MINOS, written by Robert Hatcher,
5 # but adapted to nova.
6 
7 from __future__ import print_function
8 
9 from builtins import range
10 import os, sys, getopt
11 import socket
12 import getpass, platform
13 
14 SERVER_ADDRESS = 'novabuild01.fnal.gov'
15 SERVER_PORT = 12345
16 DATAGRAM_VERSION = 'v0.1' #jpd -- introduce a version so that we know how to parse the output according to version
17 
18 # Before DATAGRAM_VERSION was introduced
19 # message = shell + ' ' + user + ' ' + release + ' ' + kernel + ' ' + sl
20 # DATAGRAM_VERSION = 'v0.1'
21 # message = shell + ' ' + user + ' ' + release + ' ' + kernel + ' ' + sl + ' ' + DATAGRAM_VERSION + ' ' + srt_public_context + ' ' + products
22 
23 
24 def Usage():
25  print("usage: datagram_client \"release\"")
26  print(" -a --addr= server address [novasoftbuild01.fnal.gov]")
27  print(" -p --port= port # [12345]")
28  print(" --shutdown request server shutdown")
29 
30 try:
31  optpairs, args = \
32  getopt.getopt(sys.argv[1:],\
33  'a:p:',\
34  ["addr=","port=","shutdown"])
35 except getopt.GetoptError:
36  Usage()
37  sys.exit(1)
38 
39 envmap = os.environ
40 if "CVMFS_DISTRO_BASE" in envmap:
41  if not os.path.isdir("/cvmfs/nova.opensciencegrid.org"):
42  sys.exit(0)
43 
44 client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
45 
46 for io in range(len(optpairs)):
47  kv = optpairs[io]
48  opt1 = kv[0]
49  arg1 = kv[1]
50  for k in ('-a','--addr'):
51  if (k == opt1):
52  SERVER_ADDRESS = arg1
53  for k in ('-p','--port'):
54  if (k == opt1):
55  SERVER_PORT = int(arg1)
56  if (opt1 == '--shutdown'):
57  args.append('SPECIAL_SHUTDOWN_MESSAGE')
58 
59 user = getpass.getuser()
60 kernel = platform.release()
61 if "SHELL" in list(os.environ.keys()):
62  shell = os.environ["SHELL"]
63 else:
64  shell = "Unknown"
65 #if SLF5 SLF6 this file exists
66 if os.path.exists('/etc/redhat-release'):
67  fin = open('/etc/redhat-release','r')
68  sl = fin.readline().split(' ')[-2]
69 #else try parsing this which can be called on OS X
70 else:
71  sl = os.popen('uname -s').read()
72 if list(os.environ.keys()).count("X509_USER_PROXY") != 0:
73  user = user + '/' + os.environ["X509_USER_PROXY"]
74 
75 #jpd Add checking of the SRT_PUBLIC_CONTEXT and PRODUCTS environment variables
76 # - only implemented in DATAGRAM_VERSION = v0.1
77 
78 if list(os.environ.keys()).count("SRT_PUBLIC_CONTEXT") != 0:
79  srt_public_context = os.environ["SRT_PUBLIC_CONTEXT"]
80 else:
81  srt_public_context = 'unknown'
82 
83 if list(os.environ.keys()).count("PRODUCTS") != 0:
84  products = os.environ["PRODUCTS"]
85 else:
86  products = 'unknown'
87 
88 for release in args:
89 
90  if release == 'SPECIAL_SHUTDOWN_MESSAGE':
91  message = 'SPECIAL_SHUTDOWN_MESSAGE'
92  client.sendto(message,(SERVER_ADDRESS, SERVER_PORT))
93  else:
94  message = shell + ' ' + user + ' ' + release + ' ' + kernel + ' ' + sl + ' ' + DATAGRAM_VERSION
95  message = message + ' ' + srt_public_context + ' ' + products
96  client.sendto(message,(SERVER_ADDRESS, SERVER_PORT))
97 
98 client.close()
void split(double tt, double *fr)
bool print
procfile open("FD_BRL_v0.txt")