#!/usr/bin/env python2.7

# Based on previous work by
# Charles Menguy (see: http://stackoverflow.com/questions/10217067/implementing-a-full-python-unix-style-daemon-process)
# and Sander Marechal (see: http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/)

# Adapted by M.Hendrix [2015]

# daemon99.py creates an XML-file and uploads data to the server.

import os, sys, shutil, glob, platform, time, commands
from libdaemon import Daemon

DEBUG = False
os.nice(8)

class MyDaemon(Daemon):
	def run(self):
		myname = os.uname()[1]
		mount_path = '/mnt/share1/'
		remote_path = mount_path + myname
		remote_lock = remote_path + '/client.lock'
		sampleptr = 0
		samples = 1

		sampleTime = 60
		cycleTime = samples * sampleTime
		# sync to whole minute
		waitTime = (cycleTime + sampleTime) - (time.time() % cycleTime)
		if DEBUG:print "Waiting {0} s".format(int(waitTime))
		time.sleep(waitTime)
		while True:
			startTime=time.time()

			if os.path.ismount(mount_path):
				#do_mv_data(remote_path)
				#do_xml(remote_path)
				do_xml('/tmp')

			waitTime = sampleTime - (time.time() - startTime) - (startTime%sampleTime)
			if (waitTime > 0):
				if DEBUG:print "Waiting {0} s".format(int(waitTime))
				time.sleep(waitTime)

def do_mv_data(rpath):
	hostlock = rpath + '/host.lock'
	clientlock = rpath + '/client.lock'
	count_internal_locks=1

	#
	#rpath='/tmp/test'
	#

	# wait 3 seconds for processes to finish
	time.sleep(3)

	while os.path.isfile(hostlock):
		# wait while the server has locked the directory
		time.sleep(1)

	# server already sets the client.lock. Do it anyway.
	lock(clientlock)

	# prevent race conditions
	while os.path.isfile(hostlock):
		# wait while the server has locked the directory
		time.sleep(1)

	while (count_internal_locks > 0):
		time.sleep(1)
		count_internal_locks=0
		for file in glob.glob(r'/tmp/*.lock'):
			count_internal_locks += 1

	for file in glob.glob(r'/tmp/*.csv'):
		#print file
		if os.path.isfile(clientlock):
			if not (os.path.isfile(rpath + "/" + os.path.split(file)[1])):
			  shutil.move(file, rpath)

	for file in glob.glob(r'/tmp/*.png'):
		if os.path.isfile(clientlock):
			if not (os.path.isfile(rpath + "/" + os.path.split(file)[1])):
				shutil.move(file, rpath)

	unlock(clientlock)

	return

def do_xml(wpath):
	#
	uname           = os.uname()
	Tcpu            = "---" #float(commands.getoutput("cat /sys/class/thermal/thermal_zone0/temp"))/1000
	fcpu            = float(commands.getoutput("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"))/1000
	synodiagdbranch = commands.getoutput("cat $HOME/.synodiagd.branch")
	uptime          = commands.getoutput("uptime")
	dfh             = commands.getoutput("df")
	freeh           = commands.getoutput("free")
	psout           = commands.getoutput("top -b -n 1 | cut -c 37- |awk 'NR>4' |sort -rnk 1 | head -10 | sed 's/&/\&amp;/g' | sed 's/>/\&gt;/g'")
	#
	f = file(wpath + '/status.txt', 'w')

	f.write('<server>\n')

	f.write('<name>\n')
	f.write(uname[1] + '\n')
	f.write('</name>\n')

	f.write('<df>\n')
	f.write(dfh + '\n')
	f.write('</df>\n')

	f.write('<temperature>\n')
	f.write(str(Tcpu) + ' degC @ '+ str(fcpu) +' MHz\n')
	f.write('</temperature>\n')

	f.write('<memusage>\n')
	f.write(freeh + '\n')
	f.write('</memusage>\n')

	f.write(' <uptime>\n')
	f.write(uptime + '\n')
	f.write(uname[0]+ ' ' +uname[1]+ ' ' +uname[2]+ ' ' +uname[3]+ ' ' +uname[4]+ ' ' +platform.platform() +'\n')
	f.write(' - synodiagd on: '+ synodiagdbranch +'\n')
	f.write('\nTop 10 processes:\n' + psout +'\n')
	f.write('</uptime>\n')

	f.write('</server>\n')

	f.close()
	return

def lock(fname):
	open(fname, 'a').close()

def unlock(fname):
	if os.path.isfile(fname):
		os.remove(fname)

if __name__ == "__main__":
	daemon = MyDaemon('/tmp/synodiagd-99.pid')
	if len(sys.argv) == 2:
		if 'start' == sys.argv[1]:
			daemon.start()
		elif 'stop' == sys.argv[1]:
			daemon.stop()
		elif 'restart' == sys.argv[1]:
			daemon.restart()
		elif 'foreground' == sys.argv[1]:
			# assist with debugging.
			print "Debug-mode started. Use <Ctrl>+C to stop."
			DEBUG = True
			daemon.run()
		else:
			print "Unknown command"
			sys.exit(2)
		sys.exit(0)
	else:
		print "usage: %s start|stop|restart|foreground" % sys.argv[0]
		sys.exit(2)

@@ -1020,17 +1020,16 @@
 th)%0A%09%09%09%09
-#
 do_xml(r
@@ -1043,27 +1043,8 @@
 ath)
-%0A%09%09%09%09do_xml('/tmp')
 %0A%0A%09%09
@@ -2906,11 +2906,11 @@
 tus.
-txt
+xml
 ', '

put XML file on server