From 8cb7f08c0a9f2e1c9b971895a89246feba69881f Mon Sep 17 00:00:00 2001 From: Björn Geiger Date: Tue, 16 Aug 2011 15:18:28 +0200 Subject: verschiedene Korrekturen --- .../controllerWorker/ControllerWorker/Boot.java | 34 ++++----- .../ControllerWorker/Shutdown.java | 34 ++++----- gearman/psWorker.py | 78 ++++++++++++++++++++ gearman/topWorker.py | 84 ---------------------- 4 files changed, 112 insertions(+), 118 deletions(-) create mode 100644 gearman/psWorker.py delete mode 100644 gearman/topWorker.py diff --git a/gearman/controllerWorker/ControllerWorker/Boot.java b/gearman/controllerWorker/ControllerWorker/Boot.java index 01cf911..54a0ede 100644 --- a/gearman/controllerWorker/ControllerWorker/Boot.java +++ b/gearman/controllerWorker/ControllerWorker/Boot.java @@ -38,7 +38,7 @@ public class Boot extends Thread { private HashMap pingWoLJobs; private HashMap osJobs; private HashMap whoJobs; - private HashMap topJobs; + private HashMap psJobs; private HashMap restartJobs; private HashMap pingRestartJobs; private HashMap status; @@ -59,7 +59,7 @@ public class Boot extends Thread { this.wolJobs = new HashMap(); this.osJobs = new HashMap(); this.whoJobs = new HashMap(); - this.topJobs = new HashMap(); + this.psJobs = new HashMap(); this.restartJobs = new HashMap(); this.pingRestartJobs = new HashMap(); this.status = new HashMap(); @@ -415,19 +415,19 @@ public class Boot extends Thread { break; case 11: - top(client); + ps(client); break; case 12: - GearmanJob checkStatusJob = topJobs.get(clientID); + GearmanJob psJob = psJobs.get(clientID); - if (checkStatusJob != null) { + if (psJob != null) { GearmanJobStatus jobStatus = gearmanClient - .getJobStatus(checkStatusJob); + .getJobStatus(psJob); - if (!jobStatus.isKnown() && checkStatusJob.isDone()) { - GearmanJobResult whoJobRes = checkStatusJob.get(); + if (!jobStatus.isKnown() && psJob.isDone()) { + GearmanJobResult whoJobRes = psJob.get(); String result = ByteUtils.fromUTF8Bytes(whoJobRes .getResults()); @@ -442,12 +442,12 @@ public class Boot extends Thread { System.out.println(ipAddress + " is working"); status.put(clientID, 18); // is working - topJobs.remove(clientID); + psJobs.remove(clientID); } else { System.out.println(ipAddress + " is not working"); status.put(clientID, 13); // is not working - topJobs.remove(clientID); + psJobs.remove(clientID); } } else { System.out.println(ipAddress @@ -458,7 +458,7 @@ public class Boot extends Thread { // cannot check if user is working, go in // errorState status.put(clientID, 19); - topJobs.remove(clientID); + psJobs.remove(clientID); } } } @@ -701,18 +701,18 @@ public class Boot extends Thread { System.out.println("who " + ipAddress); } - private void top(HashMap client) { + private void ps(HashMap client) { String ipAddress = client.get("ip"); int clientID = Integer.parseInt(client.get("id")); - GearmanJob job = GearmanJobImpl.createJob("top", ipAddress.getBytes(), - "top" + clientID); + GearmanJob job = GearmanJobImpl.createJob("ps", ipAddress.getBytes(), + "ps" + clientID); gearmanClient.submit(job); - status.put(clientID, 12); // top started - topJobs.put(clientID, job); + status.put(clientID, 12); // ps started + psJobs.put(clientID, job); - System.out.println("top " + ipAddress); + System.out.println("ps " + ipAddress); } private void restart(HashMap client) { diff --git a/gearman/controllerWorker/ControllerWorker/Shutdown.java b/gearman/controllerWorker/ControllerWorker/Shutdown.java index 35779ea..2558d64 100644 --- a/gearman/controllerWorker/ControllerWorker/Shutdown.java +++ b/gearman/controllerWorker/ControllerWorker/Shutdown.java @@ -36,7 +36,7 @@ public class Shutdown extends Thread { private HashMap whoJobs; private HashMap shutdownJobs; private HashMap pingShutdownJobs; - private HashMap topJobs; + private HashMap psJobs; private HashMap status; private HashMap errors; private HashMap pingShutdownTime; @@ -53,7 +53,7 @@ public class Shutdown extends Thread { this.whoJobs = new HashMap(); this.shutdownJobs = new HashMap(); this.pingShutdownJobs = new HashMap(); - this.topJobs = new HashMap(); + this.psJobs = new HashMap(); this.status = new HashMap(); this.errors = new HashMap(); this.updateRate = updateRate; // updates per second @@ -339,19 +339,19 @@ public class Shutdown extends Thread { break; case 9: - top(client); + ps(client); break; case 10: - GearmanJob checkStatusJob = topJobs.get(clientID); + GearmanJob psJob = psJobs.get(clientID); - if (checkStatusJob != null) { + if (psJob != null) { GearmanJobStatus jobStatus = gearmanClient - .getJobStatus(checkStatusJob); + .getJobStatus(psJob); - if (!jobStatus.isKnown() && checkStatusJob.isDone()) { - GearmanJobResult whoJobRes = checkStatusJob.get(); + if (!jobStatus.isKnown() && psJob.isDone()) { + GearmanJobResult whoJobRes = psJob.get(); String result = ByteUtils.fromUTF8Bytes(whoJobRes .getResults()); @@ -366,12 +366,12 @@ public class Shutdown extends Thread { System.out.println(ipAddress + " is working"); status.put(clientID, 11); // is working - topJobs.remove(clientID); + psJobs.remove(clientID); } else { System.out.println(ipAddress + " is not working"); status.put(clientID, 4); // is not working - topJobs.remove(clientID); + psJobs.remove(clientID); } } else { System.out.println(ipAddress @@ -382,7 +382,7 @@ public class Shutdown extends Thread { // cannot check if user is working, go in // errorState status.put(clientID, 12); - topJobs.remove(clientID); + psJobs.remove(clientID); } } } @@ -479,18 +479,18 @@ public class Shutdown extends Thread { System.out.println("ping " + ipAddress); } - private void top(HashMap client) { + private void ps(HashMap client) { String ipAddress = client.get("ip"); int clientID = Integer.parseInt(client.get("id")); - GearmanJob job = GearmanJobImpl.createJob("top", ipAddress.getBytes(), - "top" + clientID); + GearmanJob job = GearmanJobImpl.createJob("ps", ipAddress.getBytes(), + "ps" + clientID); gearmanClient.submit(job); - status.put(clientID, 9); // top started - topJobs.put(clientID, job); + status.put(clientID, 9); // ps started + psJobs.put(clientID, job); - System.out.println("top " + ipAddress); + System.out.println("ps " + ipAddress); } public String getStatusText(HashMap client) { diff --git a/gearman/psWorker.py b/gearman/psWorker.py new file mode 100644 index 0000000..b9ffb9a --- /dev/null +++ b/gearman/psWorker.py @@ -0,0 +1,78 @@ +#!/usr/bin/python + +from gearman import GearmanWorker +import subprocess +import json +import time +import re + +def psWorker(worker, job): + ret = {"rawoutput": "", "rc": -1} + target = job.data.strip() + + validIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" + validHostnameRegex = "^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$" + if not (re.match(validIpAddressRegex, target) or re.match(validHostnameRegex, target)): + print 'malformed request' + return json.dumps({"error":"invalid parameter - must be ip or host address", "alive":False}) + "\n" + + + host = target + port = 22 + user = "root" + sshopts = "-o PasswordAuthentication=no -a" + keyfile = "/etc/systemd/workers.ssh/id_rsa" + remotecmd = "ps -Al" + + cmd = "ssh -p {p} -l {l} -i {i} {o} {h} {r}".format(p=port, l=user, i=keyfile, o=sshopts, h=host, r=remotecmd) + print cmd + + + sshCommand = subprocess.Popen(["sh", "-c", "%s" % cmd], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + + while sshCommand.returncode == None: + out = sshCommand.stdout.readline() + if out != "": + ret['rawoutput'] += out + time.sleep(0.05) + sshCommand.poll() + + while True: + out = sshCommand.stdout.readline() + if out != "": + ret['rawoutput'] += out + else: + break + + ret['rawoutput'] = ret['rawoutput'].strip() # get rid of newlines + rc = ret['rc'] = sshCommand.returncode + + if rc == 0: + lines = ret['rawoutput'].splitlines() + ret['ps'] = [] + for l in xrange(len(lines)): + rowentries = lines[l].split() + if len(rowentries) == 11: + (pid, user, pr, ni, virt, res, shr_s, cpu, mem, time, command) = rowentries + data = {'pid': pid, 'user': user, 'pr': pr, 'ni': ni, 'virt': virt, 'shr_s': shr_s, 'cpu': cpu, 'mem': mem, 'time': time, 'command': command} + ret['ps'].append(data) + print 'remote command successful.' + return json.dumps(ret) + "\n" + else: + ret['err'] = "" + while True: + out = sshCommand.stderr.readline() + if out != "": + ret['err'] += out + else: + break + print 'remote command failed' + return json.dumps(ret) + "\n" + + +worker = GearmanWorker(["127.0.0.1"]) +worker.register_task('ps', psWorker) +worker.work() diff --git a/gearman/topWorker.py b/gearman/topWorker.py deleted file mode 100644 index 6a61c10..0000000 --- a/gearman/topWorker.py +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/python - -from gearman import GearmanWorker -import subprocess -import json -import time -import re - -def topWorker(worker, job): - ret = {"rawoutput": "", "rc": -1} - target = job.data.strip() - - validIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" - validHostnameRegex = "^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$" - if not (re.match(validIpAddressRegex, target) or re.match(validHostnameRegex, target)): - print 'malformed request' - return json.dumps({"error":"invalid parameter - must be ip or host address", "alive":False}) + "\n" - - - host = target - port = 22 - user = "root" - sshopts = "-o PasswordAuthentication=no -a" - keyfile = "/etc/systemd/workers.ssh/id_rsa" - remotecmd = "top" - - cmd = "ssh -p {p} -l {l} -i {i} {o} {h} {r}".format(p=port, l=user, i=keyfile, o=sshopts, h=host, r=remotecmd) - print cmd - - - sshCommand = subprocess.Popen(["sh", "-c", "%s" % cmd], - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - - while sshCommand.returncode == None: - out = sshCommand.stdout.readline() - if out != "": - ret['rawoutput'] += out - time.sleep(0.05) - sshCommand.poll() - - while True: - out = sshCommand.stdout.readline() - if out != "": - ret['rawoutput'] += out - else: - break - - ret['rawoutput'] = ret['rawoutput'].strip() # get rid of newlines - rc = ret['rc'] = sshCommand.returncode - - if rc == 0: - lines = ret['rawoutput'].splitlines() - ret['top'] = [] - for l in xrange(len(lines)): - rowentries = lines[l].split() -#-------------------------------------------# -# Hier noch die Daten des Top-Command ändern# -#-------------------------------------------# -# if len(rowentries) == 7: -# (user, tty, login_date, login_time, foo ,pid, display ) = rowentries -# if len(rowentries) == 6: -# (user, tty, login_date, login_time, foo ,pid ) = rowentries -# data = {'user': user, 'tty': tty, 'login_date':login_date, 'login_time':login_time, 'pid':pid, 'display':display} -#-------------------------------------------# - ret['top'].append(data) - print 'remote command successful.' - return json.dumps(ret) + "\n" - else: - ret['err'] = "" - while True: - out = sshCommand.stderr.readline() - if out != "": - ret['err'] += out - else: - break - print 'remote command failed' - return json.dumps(ret) + "\n" - - -worker = GearmanWorker(["127.0.0.1"]) -worker.register_task('top', topWorker) -worker.work() \ No newline at end of file -- cgit v1.2.3-55-g7522