sync met werkbranch

This commit is contained in:
Ward Wouts 2009-11-30 15:59:57 +00:00
parent 3305750de2
commit 7d36791118
4 changed files with 90 additions and 71 deletions

View file

@ -1,4 +1,4 @@
#!/usr/bin/env bash #!/usr/bin/env bash
for i in $(cat targets);do for i in $(cat targets);do
./pb add nmap -PN -v -r -sS -sV --version-light --reason -O -T4 -oN nmap-sS.${i}.txt ${i} ./pb.rb add nmap -PN -v -r -sS -sV --version-light --reason -O -T4 -oN nmap-sS.${i}.txt ${i}
done done

View file

@ -1,4 +1,4 @@
#!/usr/bin/env bash #!/usr/bin/env bash
for i in $(cat targets);do for i in $(cat targets);do
./pb add nmap -PN -v -r -sU -sV --version-light --reason -T4 -oN nmap-sU.${i}.txt ${i} ./pb.rb add nmap -PN -v -r -sU -sV --version-light --reason -T4 -oN nmap-sU.${i}.txt ${i}
done done

View file

@ -4,6 +4,8 @@
# $URL$ # $URL$
require 'fileutils' require 'fileutils'
require 'thread'
require 'open3'
JOBSDIR = "jobs" JOBSDIR = "jobs"
PARKDIR = "park" PARKDIR = "park"
@ -14,6 +16,8 @@ MONITORDIR = "monitor"
RUNMODE = 0755 RUNMODE = 0755
CONFIG = "config" CONFIG = "config"
@config = { "waittime" => 3, "maxproc" => 3 } @config = { "waittime" => 3, "maxproc" => 3 }
@processor_threads = []
@jobclaimlock = Mutex.new
COUNTERFILE="jobscounter" COUNTERFILE="jobscounter"
RUNNINGFILE="runningcounter" RUNNINGFILE="runningcounter"
@ -22,19 +26,19 @@ def usage
puts <<ENDTXT puts <<ENDTXT
Usage: pb <option> Usage: pb <option>
add <command>>-->-------Add the given command as a job. add <command> Add the given command as a job.
addfile|af <file>>------Add the given file as a job. addfile|af <file> Add the given file as a job.
createconfing>-->-------Write a default configuration file. createconfing Write a default configuration file.
run>---->------->-------Start the master scheduler process. run Start the master scheduler process.
del <jobnr>>---->-------Delete the given job from the queue. del <jobnr> Delete the given job from the queue.
help|-h>>------->-------Show this help. help|-h Show this help.
park>--->------->-------Place all queued jobs in the parkinglot. park Place all queued jobs in the parkinglot.
unpark>->------->-------Place all jobs from the parkinglot back in the queue. unpark Place all jobs from the parkinglot back in the queue.
retry <jobnr>>-->-------Reschedule a failed job. Destroys all output. retry <jobnr> Reschedule a failed job. Destroys all output.
prio <h|n|l> <jobnr>>---Change the priority of the given job.>--[unimplemented] prio <h|n|l> <jobnr> Change the priority of the given job. [unimplemented]
status|st>------>-------Show the status of the current jobs. status|st Show the status of the current jobs.
stop <jobnr>>--->-------Stop the given job.>----[unimplemented] stop <jobnr> Stop the given job. [unimplemented]
stopall>>------->-------Stop all running jobs.>-[unimplemented] stopall Stop all running jobs. [unimplemented]
ENDTXT ENDTXT
exit exit
end end
@ -136,7 +140,7 @@ def addjobfromfile
end end
jobnumber = sprintf("%06d", increasejobscounter) jobnumber = sprintf("%06d", increasejobscounter)
puts "New job: #{jobnumber}" puts "New job: #{jobnumber}"
File.copy(filename, "#{@basedir}/#{JOBSDIR}/#{jobnumber}") FileUtils.cp(filename, "#{@basedir}/#{JOBSDIR}/#{jobnumber}")
File.chmod(RUNMODE, "#{@basedir}/#{JOBSDIR}/#{jobnumber}") File.chmod(RUNMODE, "#{@basedir}/#{JOBSDIR}/#{jobnumber}")
end end
@ -209,35 +213,6 @@ def changeprio
puts "unimplemented" puts "unimplemented"
end end
def incrementrunning
# als ik threading ga gebruiken is dit niet meer nodig (want geen runningfile nodig)
File.new(RUNNINGFILE).flock(File::LOCK_EX)
File.open(RUNNINGFILE, "r+"){|file|
running = file.read
running = running.to_i + 1
file.seek(0)
file.trunc
file.puts running
}
File.new(RUNNINGFILE).flock(File::LOCK_UN)
end
def decrementrunning
# als ik threading ga gebruiken is dit niet meer nodig (want geen runningfile nodig)
File.new(RUNNINGFILE).flock(File::LOCK_EX)
File.open(RUNNINGFILE, "r+"){|file|
running = file.read
running = running.to_i - 1
if running < 0
running = 0
end
file.seek(0)
file.trunc
file.puts running
}
File.new(RUNNINGFILE).flock(File::LOCK_UN)
end
def getjob def getjob
Dir.entries("#{@basedir}/#{JOBSDIR}").each{|file| Dir.entries("#{@basedir}/#{JOBSDIR}").each{|file|
if file == '.' or file == '..' if file == '.' or file == '..'
@ -250,6 +225,7 @@ def getjob
end end
end end
} }
return nil
end end
def failjob(jobnumber) def failjob(jobnumber)
@ -261,24 +237,46 @@ def finishjob(jobnumber)
end end
def runprocessor def runprocessor
incrementrunning @processor_threads << Thread.new do
sleep 1 # XXX niet nodig als locking echt werkt (of bij threading) begin
# gebruik een mutex voor het claimen van een job!
runjob = nil
@jobclaimlock.synchronize {
runjob = getjob runjob = getjob
}
if ! runjob.nil? if ! runjob.nil?
starttime = Time.now
puts "Starting job: #{runjob}" puts "Starting job: #{runjob}"
owd = Dir.pwd owd = Dir.pwd
Dir.cd("#{@basedir}/#{PROCDIR}/#{runjob}") # puts "#{@basedir}/#{PROCDIR}/#{runjob}"
# XXX IPC nog even uitzoeken... popen3 ? Dir.chdir("#{@basedir}/#{PROCDIR}/#{runjob}")
# if "${basedir}/processors/${runjob}/job" 2>&1 > "${basedir}/processors/${runjob}/output" ;then begin
# finishjob ${runjob} output = File.open("#{@basedir}/#{PROCDIR}/#{runjob}/output", "w")
puts "Finished job: #{runjob}" Open3.popen3("#{@basedir}/#{PROCDIR}/#{runjob}/job", "r+"){ |stdin, stdout, stderr|
# else # dit moet netter kunnen, nu wordt de volgorde van de diverse outputs veranderd
# failjob ${runjob} while !stdout.eof and ! stderr.eof do
puts "Failed job: #{runjob}" #output.print "XXX ERR #{stderr.gets}"
# fi #output.print "XXX OUT #{stdout.gets}"
Dir.cd(owd) output.print stderr.gets
output.print stdout.gets
end
}
output.close
finishjob runjob
puts "Finished job: #{runjob} [runtime: #{Time.now-starttime}]"
rescue
failjob runjob
puts "Failed job: #{runjob} [runtime: #{Time.now-starttime}]"
puts $!
end
Dir.chdir(owd)
end
rescue
puts "failure in thread"
puts $!
end
end end
decrementrunning
end end
def stop def stop
@ -291,18 +289,27 @@ end
def master def master
if Process.euid != 0 if Process.euid != 0
puts "If your jobs need root priviledges (XXX spelling) you want to run this as root" puts "If your jobs need root privileges you want to run this as root"
end end
# als ik threading ga gebruiken is dit niet meer nodig (want geen runningfile nodig) while true
if ! FileTest.file?(RUNNINGFILE) sleep @config["waittime"]
FileUtils.touch(RUNNINGFILE) # puts "Number of threads: #{@processor_threads.length}"
# dode threads moeten opgeruimd worden
(0...@processor_threads.length).to_a.reverse.each{|thr|
# puts "thr: #{thr}"
# puts @processor_threads[thr].status
if @processor_threads[thr].status == false
# puts "deleting thread #{thr}"
@processor_threads.delete_at(thr)
end end
File.new(RUNNINGFILE).flock(File::LOCK_EX)
File.open(RUNNINGFILE, "w"){|file|
file.puts "0"
} }
File.new(RUNNINGFILE).flock(File::LOCK_UN) if @processor_threads.length < @config["maxproc"]
# nog testen op aanwezig zijn van nieuwe jobs in queue?
runprocessor
end
readconfig
end
# while sleep $waittime; do # while sleep $waittime; do
# if [ $(cat "${basedir}/${runningfile}") -lt $maxproc ]; then # if [ $(cat "${basedir}/${runningfile}") -lt $maxproc ]; then

12
pbatch/stderrandout.rb Executable file
View file

@ -0,0 +1,12 @@
#!/usr/bin/env ruby
$stderr.puts "STANDARD ERROR 1"
$stderr.puts "STANDARD ERROR 2"
$stderr.puts "STANDARD ERROR 3"
$stderr.puts "STANDARD ERROR 4"
$stderr.puts "STANDARD ERROR 5"
$stdout.puts "STANDARD OUT 1"
$stdout.puts "STANDARD OUT 2"
$stdout.puts "STANDARD OUT 3"
$stdout.puts "STANDARD OUT 4"
$stdout.puts "STANDARD OUT 5"