log-uploader 2.55 KB
Newer Older
1 2
#!/usr/bin/python2.7

Kristopher Ruzic's avatar
Kristopher Ruzic committed
3
import os
4 5
import sys
import time
6
from glob import glob
7
import requests
8 9 10 11 12 13 14
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class MyHandler(FileSystemEventHandler):
  def on_created(self, event):
    if ".completed" in event.file:
      print "partition completed!"
15
      upload_file(event.file, fmt_date() + ": partition completed")
16 17 18 19

def fmt_date():
  return time.strftime("[ %Y%m%d %H:%M:%S ]")

20 21 22
def dir_date():
  return time.strftime("%Y%m%d/")

23
class uploader():
24 25 26 27 28
  url = 'http://[2001:67c:1254:e:a9::bafb]:9000/post'
  # takes a file name and contents to append. Generates server file_path (where it should be saved on server)
  def upload_file(self, file_name, contents):
    values = {'file_path': dir_date() + file_name, 'contents': contents}
    r = requests.post(self.url, data=values)
29

30 31 32 33 34 35 36 37 38 39 40
def check_startup():
  pid = str(os.getpid())
  pidfile = "/tmp/slaplogger.pid"

  if os.path.isfile(pidfile):
    print "%s already exists, exiting" % pidfile
    sys.exit()
  else:
    file(pidfile, 'w').write(pid)
  os.unlink(pidfile)

Kristopher Ruzic's avatar
Kristopher Ruzic committed
41 42
def scan_files(files):
  try:  
43
    with open("/opt/slapos/lines", 'r') as myfile:
Kristopher Ruzic's avatar
Kristopher Ruzic committed
44 45 46
      software_line = myfile.readlines()
  except:
    software_line = [0] * len(files)
47
  with open("/opt/slapos/lines", 'w'):
Kristopher Ruzic's avatar
Kristopher Ruzic committed
48 49 50 51 52 53 54 55
    pass  
  place = 0

  for f in files:
    print software_line[place]
    print software_line
    with open(f, 'r') as myfile:
      new_lines = myfile.readlines()[software_line[place]:]
56
    with open("/opt/slapos/lines", 'a') as myfile:
Kristopher Ruzic's avatar
Kristopher Ruzic committed
57 58 59 60 61 62 63
      myfile.write(str(software_line[place]) + str(len(new_lines)))
    print "Uploading " + f + "..."
    c.upload_file(os.path.basename(f), new_lines)
    print f + " finished uploading!"
    place += 1
  print "Files uploaded"

64 65

if __name__ == "__main__":
66
  check_startup()
67
  c = uploader()
Kristopher Ruzic's avatar
Kristopher Ruzic committed
68 69
  files = ["/opt/slapos/log/slapos-node-software.log", "/opt/slapos/log/slapos-node-format.log", "/opt/slapos/log/slapos-node-instance.log"]

70 71 72 73 74 75 76 77
  # first check if .completed exists in any partitions
  paths = ["/opt/slapgrid/slappart" + p + ".completed" for p in glob('/opt/slapgrid/*')]
  for p in paths: 
    try:
      f = open(p, 'r')
      upload_file(p, fmt_date() + ": partition completed")    
    except IOError as e:
      pass
78

79 80
  event_handler = MyHandler()
  observer = Observer()
81 82 83


  # check if .completed exists or not, and write it to the server log
84 85 86 87 88 89 90 91 92
  observer.schedule(event_handler, path='/opt/slapgrid/', recursive=True)
  observer.start()

  try:
    while True:
      scan.files(files)
  except KeyboardInterrupt:
    observer.stop()
  observer.join()