log-uploader 2.24 KB

import os
import sys
import time
import requests
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class MyHandler(FileSystemEventHandler):
  def on_modified(self, event):
    print vars(event)
  def on_created(self, event):
    if ".completed" in event.file:
      print "partition completed!"
      f = open("tmp/tmpupload", 'w')
      f.write(fmt_date() + ": partition completed") 
      upload_file(event.file, open("tmp/tmpupload", 'rb'))

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

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

class uploader():
  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)

def scan_files(files):
    with open("lines", 'r') as myfile:
      software_line = myfile.readlines()
    software_line = [0] * len(files)
  with open("lines", 'w'):
  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]:]
    with open("lines", 'a') as myfile:
      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"

if __name__ == "__main__":
  c = uploader()
  files = ["/opt/slapos/log/slapos-node-software.log", "/opt/slapos/log/slapos-node-format.log", "/opt/slapos/log/slapos-node-instance.log"]

 #  event_handler = MyHandler()
 # observer = Observer()
 # observer.schedule(event_handler, path='/opt/slapos/log', recursive=False)
 # observer.start()

  # check if .completed exists or not, and write it to the server log
# """ observer.schedule(event_handler, path='/opt/slapgrid/', recursive=True)
 # observer.start()

#  try:
 #   while True:
  #    time.sleep(1)
 # except KeyboardInterrupt:
 #   observer.stop()
 # observer.join()