#!/usr/bin/perl # Deklarasjon av diverse viktigheter :) require strict; require '/var/www-includes/db.inc'; require '/var/www-includes/common.inc'; require CGI; my $query = CGI::new(); use Benchmark; use Time::HiRes; use CGI::Carp qw/fatalsToBrowser/; my $section = $query->param('section'); my $subsection = $query->param('subsection'); my $limit = $query->param('limit'); my $time0 = new Benchmark; my $id = $query->param('id'); my $cnt = 0; my $visitor_ip = $ENV{'REMOTE_ADDR'}; my $current_time = localtime(); my $Script = $ENV{'SCRIPT_NAME'}; my $CSS = '/inc/xhtml-test.css'; my $title = 'Smokepit.net:/'; # Start sidekode.. if(&db_connect('webpage')) { &CheckQuery; &PrintPage; } # if() sub EndHTML { print '
'; my $time1 = new Benchmark; print '

',"Processing time: ".timestr(timediff($time1,$time0)),'

',"\n"; print '

','Local time: ',$current_time,' - ','Uptime: ',&gettime(`cat /proc/uptime | awk '{print \$1}'`),'

'; print '

','Your IP: ',$visitor_ip,' - ',"\n"; &LagLink('/inc/index.txt','PerlSource'); print '

',"\n",'

',"\©",'Daniel Lysfjord

',"\n"; print '',"\n"; print '',"\n"; print '',"\n\n"; print '',"\n"; } sub PrintPage { &StartHTML($title,$section,$subsection,$CSS); print '',"\n\n"; unless(&GetNav) { &error('Ooooops.. Something wicked happened when trying to get the menu.'); } # unless() print '',"\n\n"; unless($section eq 'news') { print '',"\n\n"; unless(&LatestNews) { &error('Could not get the latest news.'); } # unless() print '',"\n\n"; } # unless() print '',"\n\n"; print '
',"\n"; unless(&$section) { &error('Execution of section '.$section.'failed.'); } # unless() print '',"\n\n"; &EndHTML; } # sub PrintPage() sub GetNav { print '',"\n"; return 1; } # sub GetNav() sub main { print '

Welcome to Smokepit.net

',"\n"; &db (" SELECT content FROM content WHERE section = \"main\" ORDER BY date, time "); while(my $ref = $sth->fetchrow_hashref) { print $$ref{'content'}; print "\n"; } # while() print '

-='; &LagLink('?section=people&id=1','Daniel Lysfjord'); print '=-

',"\n"; return(1); } # sub main() sub CheckQuery { unless($section) { $section = 'main'; } # unless() unless($limit) { $limit = "3"; } # unless() unless($subsection) { $subsection = ''; } # unless() } # sub CheckQuery sub LatestNews { &db (" SELECT date, time, section, content FROM content WHERE section = \"news\" OR section = \"syslog\" ORDER BY date DESC, time DESC LIMIT 3 "); print '
',"\n",'

Latest News:

',"\n"; while(my $ref = $sth->fetchrow_hashref) { print '

',$$ref{'date'},'/',$$ref{'time'},'

',"\n",'

',$$ref{'content'},'

',"\n"; } # while() print '
',"\n",'

',"\n",'Read all from :','

',"\n",'

',"\n"; &LagLink('?section=news&limit=999&subsection=news','news'); print ' - ',"\n"; &LagLink('?section=news&limit=999&subsection=syslog','syslog'); print '

',"\n",'
',"\n"; return(1); } # sub LatestNews sub news { &db (" SELECT date, time, section, content FROM content WHERE section = \"$subsection\" ORDER BY date DESC, time DESC LIMIT $limit "); print '

',"\U$subsection",':','

',"\n"; while(my $ref = $sth->fetchrow_hashref) { if($$ref{'date'}) { print '

',$$ref{'date'},'/',$$ref{'time'},' CEST:

',"\n"; } # if() print '

',$$ref{'content'},'

',"\n"; } # while() return(1); } # sub news() sub archives { &db (" SELECT DISTINCT section FROM content WHERE section != \"main\" "); while(my $ref = $sth->fetchrow_hashref) { print '

'; &LagLink('?section=news&subsection='.$$ref{'section'}.'&limit=999',$$ref{'section'}.'archive'); print '

',"\n"; } # while() return(1); } # sub archives() sub irc { unless($subsection) { $subsection = "main"; } # unless() &db (" SELECT content FROM irc WHERE section = \"$subsection\" "); while(my $ref = $sth->fetchrow_hashref) { print $$ref{'content'}; print "\n"; } # while() return(1); } # sub irc() sub sysinfo { if($subsection eq 'services') { &db (" SELECT services.Program AS programname, services.port AS portnumber, services.name AS servicename, services.machineid AS machineid, machines.name AS machinename FROM services, machines WHERE services.machineid = machines.id ORDER BY port "); print '

'; print 'Externally available services :'; print '

'; print "\n"; while(my $ref = $sth->fetchrow_hashref) { my $cnt = 0; print '

','Port : '; print $$ref{'portnumber'}; print '('; print $$ref{'programname'}; print ')',', Machine : '; &LagLink('?section=sysinfo&subsection=machines&id='.$$ref{'machineid'},$$ref{'machinename'}); print '

',"\n"; } # while() return(1); } # if() if($subsection eq "machines") { &db (" SELECT id, name, description FROM machines ORDER BY name "); print '

'; print 'Machines here at smokepit :'; print '

'; print "\n"; while(my $ref = $sth->fetchrow_hashref) { print '

'; &LagLink('?section=sysinfo&subsection=machines&id='.$$ref{'id'},$$ref{'name'}); print ': '; print $$ref{'description'}; print '

'; print "\n"; } # while() if($id) { &db (" SELECT name, cpu, ram, nic, controllers, disks, img FROM machines WHERE id = \"$id\" "); print "\n",'
'; while(my $ref2 = $sth -> fetchrow_hashref) { print '

'; print 'Name:'; print '

'; print '

'; print "\n"; print $$ref2{'name'}; print "\n"; print '

'; print "\n"; if(defined($$ref2{'cpu'})) { print '

'; print 'CPU:'; print '

'; print '

'; print "\n"; print $$ref2{'cpu'}; print "\n"; print '

'; print "\n"; } if(defined($$ref2{'ram'})) { print '

'; print 'RAM:'; print '

'; print '

'; print "\n"; print $$ref2{'ram'}; print "\n"; print '

'; print "\n"; } if(defined($$ref2{'nic'})) { print '

'; print 'Nic:'; print '

'; print '

'; print "\n"; print $$ref2{'nic'}; print "\n"; print '

'; print "\n"; } if(defined($$ref2{'controllers'})) { print '

'; print 'Controllers:'; print '

'; print '

'; print "\n"; print $$ref2{'controllers'}; print "\n"; print '

'; print "\n"; } if(defined($$ref2{'disks'})) { print '

'; print 'Disks:'; print '

'; print '

'; print "\n"; print $$ref2{'disks'}; print "\n"; print '

'; print "\n"; } if($$ref2{'img'}) { print '
',"\n"; print '

'; print "\n"; print '',$$ref2{'name'},''; print "\n"; print '

'; } print "\n"; } } # unless() return(1); } # if() unless($subsection) { print '

','System Information','

',"\n"; unless(&links("sysinfo")) { &error("Error getting links for sysinfo."); } print '
',"\n"; # use Sys::CpuLoad; # use Sys::Hostname; # my $host = hostname; # use Linux::Cpuinfo; use grapher; # use Linux::meminfo; # my %mem = get_meminfo; # my $cpu = Linux::Cpuinfo->new(); # my $sysinfo_uptime = `cat /proc/uptime | awk '{print \$1}'`; # my $sysinfo_kernel_release = `uname -r`; # my $sysinfo_memory_total = $mem{'MemTotal'}; # my $sysinfo_memory_free = $mem{'MemFree'}; # my $sysinfo_memory_buffers = $mem{'Buffers'}; # my $sysinfo_memory_cached = $mem{'Cached'}; # my $sysinfo_users_num_logged_in = `users | wc -w | sed 's/^[ \t]*//'`; # my $sysinfo_users_total_logins = `last | grep -v reboot | wc -l`; # my $sysinfo_procs_num = `ps ax | sed 1d | wc -l`; # chomp($sysinfo_kernel_release); # chomp($sysinfo_procs_num); # chomp($sysinfo_users_total_logins); # my $ub = `cat /var/www/inc/ubar.txt`; # $ub =~ s/[a-z]//gi; # $ub =~ s/[\/]//g; # $ub_percent = &get_percent($ub,1300); # my $sysinfo_memory_used = $mem{'MemTotal'} - $mem{'MemFree'} - $mem{'Buffers'} - $mem{'Cached'}; # my $sysinfo_memory_used_percent = &get_percent($sysinfo_memory_used,$mem{'MemTotal'}); # my $sysinfo_memory_free_percent = &get_percent($mem{'MemFree'},$mem{'MemTotal'}); # my $sysinfo_memory_buffers_percent = &get_percent($mem{'Buffers'},$mem{'MemTotal'}); # my $sysinfo_memory_cached_percent = &get_percent($mem{'Cached'},$mem{'MemTotal'}); # $sysinfo_uptime = &gettime($sysinfo_uptime); # $sysinfo_users_total_logins = $sysinfo_users_total_logins - 2; # Print systeminformasjonen.. # print '

',"\n",'Local hostname : ',$host,"\n",'

',"\n"; # print '

',"\n",'CPU : ',$cpu->vendor_id(),' ',$cpu->model_name(),' ',$cpu->cpu_mhz(),'MHz (',$cpu->bogomips(),' bogomips)','

',"\n"; # print '

',"\n",'Kernel : ',$sysinfo_kernel_release; # print ' - Uptime : ',$sysinfo_uptime,"\n",'

',"\n"; # print '

',"\n",'Logged in users : ',$sysinfo_users_num_logged_in; # print ' - ','Total logins this month : ',$sysinfo_users_total_logins,"\n",'

',"\n"; # print '

','Load average : ', # join(' , ', Sys::CpuLoad::load()); # print "\n",'(',$sysinfo_procs_num,' processes)','

', "\n"; # print '

','Memory :','

',"\n"; # print '
',"\n"; # print '

','Total : ',$mem{'MemTotal'},'kb','

',"\n"; # print '

','Free : ',$mem{'MemFree'},'kb','(',$sysinfo_memory_free_percent,'%)','

',"\n"; # print '

','Buffers : ',$mem{'Buffers'},'kb','(',$sysinfo_memory_buffers_percent,'%)','

',"\n"; # print '

','Cached : ',$mem{'Cached'},'kb','(',$sysinfo_memory_cached_percent,'%)','

',"\n"; # print '

','Used : ',$sysinfo_memory_used,'kb','(',$sysinfo_memory_used_percent,'%)',"\n",'

',"\n"; # print '
',"\n"; print &generate_load("host=loke mode=monthly nolink=1"),"\n"; print &generate_mem("host=loke mode=monthly nolink=1"),"\n"; print '

','Last uptimes :','

'; &get_uptimes; print "

Diskusage :

\n"; print '
',"\n";
		
		open(DF, 'LANG=C df -Ph|');
		my @df = ;
		close(DF);
		shift(@df);
		foreach(sort(@df))
		{
			chomp;
			my @line	= split;
			unless($line[5] =~ /dev|lib/)
			{
				print sprintf("%-15s", $line[5]);
				$line[4]	=~ s/\D//g;
				my $dfp		= $line[4] / 5;
				my $tst;
				for $tst(0..20)
				{
					if($tst < $dfp)
					{
						print "=";
					}
					else
					{
						print "-";
					}
					$tst++;
				}
				print sprintf("%6s", $line[2]).'/'.sprintf("%5s", $line[1]),' (',sprintf("%3s", $line[4]),"%)\n";
			}
		}
		print '
',"\n"; # ferdig med utprinting av systeminformasjon... return(1); } # if() } # sub sysinfo() sub get_uptimes { my $dbh2 = DBI->connect("DBI:mysql:database=sysinfo;host=loke","sql-read","",{'RaiseError' => 0}); my $sth2 = $dbh2->prepare (" SELECT * FROM uptime WHERE host = 'loke' ORDER BY boot DESC LIMIT 10 "); $sth2->execute; print '
Boot			   |		Uptime
'; print '----------------------------------------------
'; my $rii = 0; while(my $reffie = $sth2->fetchrow_hashref) { if($rii == 0) { print ''; } print scalar localtime($$reffie{'boot'})," | ",sprintf("%17s", &gettime($$reffie{'uptime'})),"\n"; if($rii == 0) { print ''; $rii++; } } print '
'; } sub people { unless($id) { print '

','id is required.. Error in query','

'; } # # &db #(" #SELECT DISTINCT # id, # name #FROM # people #"); # $numFields = $sth->{'NUM_OF_FIELDS'}; # # while(my $ref = $sth->fetchrow_hashref) # { # print '

'; # &LagLink('?section=people&id='.$$ref{'id'},$$ref{'name'}); # print '

'; # print "\n"; # } # while() # return(1); # } # unless() # else { &db (" SELECT name, email, homepage FROM people WHERE id = \"$id\" "); $numFields = $sth->{'NUM_OF_FIELDS'}; while(my $ref = $sth->fetchrow_hashref) { print '

'; print "\U$$ref{'name'}"; print '

'; print "\n"; if($$ref{'email'}) { my $email = (split /@/, $$ref{'email'})[0]; my $domain = (split /@/, $$ref{'email'})[1]; $email = $email.'@anti.spam.'.$domain; print '

'; print 'Email : '; &LagLink('mailto:'.$email,$email); print '

'; print "\n"; } # unless() if($$ref{'homepage'}) { print '

'; print "Homepage : "; &LagLink($$ref{'homepage'},$$ref{'homepage'}); print '

'; print "\n"; } # if() } # while() print '

',"\n"; # print '

',"\n"; # &LagLink('?section=people','Browse'); # print ' the people database',"\n"; # print '

',"\n"; return(1); } # else() } # sub people() sub services { &db (" SELECT * FROM services "); while(my $ref = $sth->fetchrow_hashref) { print '

'; print "\U$$ref{'name'}"; print '

'; print "\n"; print '

'; print $$ref{'description'}; print '

'; } return 1; }