diff options
| author | Paul Buetow (mars.fritz.box) <paul@buetow.org> | 2014-03-02 13:31:56 +0100 |
|---|---|---|
| committer | Paul Buetow (mars.fritz.box) <paul@buetow.org> | 2014-03-02 13:31:56 +0100 |
| commit | a647d21b81115ef6a81b5644936b725a9aa3bb8e (patch) | |
| tree | eef72bbae11fc3fad70dad004ccbabfb134921d2 | |
| parent | da03c49e263ad9562253a01217e2fe5166c97d98 (diff) | |
initial working release
| -rw-r--r-- | Makefile | 5 | ||||
| -rw-r--r-- | debian/guprecords.substvars | 1 | ||||
| -rw-r--r-- | docs/guprecords.1 | 170 | ||||
| -rw-r--r-- | docs/guprecords.pod | 114 | ||||
| -rw-r--r-- | docs/guprecords.txt | 132 | ||||
| -rwxr-xr-x | src/guprecords | 158 |
6 files changed, 278 insertions, 302 deletions
@@ -6,7 +6,7 @@ all: version documentation build # Builds the project. Since this is only a fake project, it just copies a script. build: cp -p ./src/$(NAME) bin/$(NAME) - echo "echo This is version $$(cat .version)" >> bin/$(NAME) + sed -i 's/VERSION_DEVEL/$$(cat .version)/' bin/$(NAME) # 'install' installes a fake-root, which will be used to build the Debian package # $DESTDIR is actually set by the Debian tools. @@ -42,6 +42,9 @@ deb: all dch: dch -i +dput: + dput -u wheezy-buetowdotorg ../$(NAME)_$$(cat ./.version)_amd64.changes + release: dch deb bash -c "git tag $$(cat .version)" git push --tags diff --git a/debian/guprecords.substvars b/debian/guprecords.substvars index abd3ebe..bcb0957 100644 --- a/debian/guprecords.substvars +++ b/debian/guprecords.substvars @@ -1 +1,2 @@ +perl:Depends=perl misc:Depends= diff --git a/docs/guprecords.1 b/docs/guprecords.1 index ab84c6c..06050bd 100644 --- a/docs/guprecords.1 +++ b/docs/guprecords.1 @@ -123,146 +123,70 @@ .rm #[ #] #H #V #F C .\" ======================================================================== .\" -.IX Title "FOO 1" -.TH FOO 1 "2014-02-03" "guprecords 0.0.0.0" "User Commands" +.IX Title "GUPRECORDS 1" +.TH GUPRECORDS 1 "2014-03-02" "guprecords 0.0.0.0" "User Commands" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" -guprecords \- A template project -.SH "WTF?" -.IX Header "WTF?" -You may use this project in order to build your own Debian package for your own project. This has been tested with Debian GNU/Linux Squeeze and Debian GNU/Linux Wheezy. -.PP -Feel free to modify any aspects. This project is just an empty example guprecords. -.PP -Follow these steps: -.SS "Install required packages" -.IX Subsection "Install required packages" -Run the following: +guprecords \- Global uptime records +.PP +Shows uprecord stats of several hosts at once. +.SS "Synopsis" +.IX Subsection "Synopsis" +guprecords [\-\-help] [\-\-total|\-\-all] [\-\-count=i] [\-\-indir=s] +.SS "Parameters" +.IX Subsection "Parameters" +.IP "\-\-help" 4 +.IX Item "--help" +Shows the help +.IP "\-\-total" 4 +.IX Item "--total" +Aggregates a total uptime for every single host. +.IP "\-\-all" 4 +.IX Item "--all" +Shows every individual uptime of all hosts. +.IP "\-\-count=i" 4 +.IX Item "--count=i" +Show i num of entries. Default is 23. +.IP "\-\-indir=s" 4 +.IX Item "--indir=s" +Read all the *.records files from dir s. Default is ./ +.SS "Quick getting started" +.IX Subsection "Quick getting started" +\fIUptimed\fR +.IX Subsection "Uptimed" +.PP +Firstival, you need to collect uprecords using the uptimed deaemon. To install it run: .PP .Vb 1 -\& sudo aptitude install lintian devscripts dpkg\-dev make perl +\& sudo aptitude install uptimed .Ve .PP -Todo: Ensure this are the correct packages. In order to test that I would have to setup a blank Debian system. -.SS "Compile the project" -.IX Subsection "Compile the project" -Go to the to level directory and run -.PP -.Vb 1 -\& make -.Ve +Please consult the uptimed and uprecords manpages. Please ensure to understand how it works and what it does. .PP -To test run +uptimed collects uprecords to .PP .Vb 1 -\& ./bin/guprecords +\& /var/spool/uptimed/records .Ve .PP -It should print out the version number of the project. -.SS "Create a Debian package" -.IX Subsection "Create a Debian package" -Go to the to level directory and run -.PP -.Vb 1 -\& make deb -.Ve -.PP -It will create the files like: -.PP -.Vb 4 -\& ../guprecords_0.0.0.0_all.deb -\& ../guprecords_0.0.0.0.dsc -\& ../guprecords_0.0.0.0_amd64.changes -\& ../guprecords_0.0.0.0.tar.gz -.Ve +And this file is used by guprecords for further processing. .PP -It should create a debian package in ../. Check and install it, e.g: -.PP -.Vb 2 -\& lintian \-\-pedantic ../guprecords_0.0.0.0_all.deb -\& sudo dpkg \-i ../guprecords_0.0.0.0_all.deb -.Ve -.PP -Run -.PP -.Vb 1 -\& dpkg \-L guprecords -.Ve -.PP -to see whats in there. You can now run -.PP -.Vb 1 -\& /usr/bin/guprecords -.Ve +\fICollect all the uprecords\fR +.IX Subsection "Collect all the uprecords" .PP -or for example +You may have several hosts with uptimed running already. Collect all the records file to a central repository (e.g. git). Name each file \s-1FQDN\s0.records and run .PP .Vb 1 -\& man guprecords +\& guprecords \-\-indir ./ .Ve -.SS "Read the Makefile" -.IX Subsection "Read the Makefile" -Read the Makefile in order to understand what's going on. -.SH "Customize" -.IX Header "Customize" -Now, since you understood everything feel free to customize everything the way you want it. E.g.: .PP -.Vb 1 -\& Don\*(Aqt use POD for documentation but LaTeX -\& -\& Compile a C program -\& -\& Include a ./lib dir, add it to the \*(Aqinstall\*(Aq Makefile rule -\& -\& etc etc. -.Ve -.PP -You should also consider the following: -.SS "Manual page" -.IX Subsection "Manual page" -This template is using \s-1POD\s0 for creating manual pages. Edit ./docs/guprecords.pod and run -.PP -.Vb 1 -\& make documentation -.Ve -.PP -in order to build ./docs/guprecords.1. The page will be included in the resulting debian package automatically. You can review the page with -.PP -.Vb 1 -\& man ./docs/guprecords.1 -.Ve -.SS "Renaming guprecords into your project name" -.IX Subsection "Renaming guprecords into your project name" -Rename all files which have *guprecords* included into your own new package name. You can do that with: -.PP -.Vb 3 -\& PROJECTNAME=yourproject -\& find . \-name \e*guprecords\e* | -\& while read guprecords; do git mv $guprecords ${guprecords/guprecords/$PROJECTNAME}; done -.Ve -.PP -Search all content and rename *guprecords* into your own new package name. You can do that with: -.PP -.Vb 2 -\& grep \-R guprecords . | grep \-v .git | -\& cut \-d: \-f1 | uniq | xargs sed \-i "s/guprecords/$PROJECTNAME/g" -.Ve -.SS "Updating ./debian" -.IX Subsection "Updating ./debian" -Edit the following files accordingly to your new project (e.g. with vim): -.PP -.Vb 3 -\& ./debian/control -\& ./debian/copyright -\& ./debian/README -.Ve -.SS "Update changelog" -.IX Subsection "Update changelog" -Go to the to level directory and run -.PP -.Vb 1 -\& dch \-i -.Ve +You may automate the collecting of all the uprecords using something like cron or puppet. +.SH "LICENSE" +.IX Header "LICENSE" +Se package description or project website. +.SH "AUTHOR" +.IX Header "AUTHOR" +Paul Buetow \- <http://guprecords.buetow.org> diff --git a/docs/guprecords.pod b/docs/guprecords.pod index 29ff2bd..951936a 100644 --- a/docs/guprecords.pod +++ b/docs/guprecords.pod @@ -1,116 +1,70 @@ =head1 NAME -guprecords - A template project +guprecords - Global uptime records -=head1 WTF? +Shows uprecord stats of several hosts at once. -You may use this project in order to build your own Debian package for your own project. This has been tested with Debian GNU/Linux Squeeze and Debian GNU/Linux Wheezy. +=head2 Synopsis -Feel free to modify any aspects. This project is just an empty example guprecords. +guprecords [--help] [--total|--all] [--count=i] [--indir=s] -Follow these steps: +=head2 Parameters -=head2 Install required packages +=over -Run the following: +=item --help - sudo aptitude install lintian devscripts dpkg-dev make perl +Shows the help -Todo: Ensure this are the correct packages. In order to test that I would have to setup a blank Debian system. +=item --total -=head2 Compile the project +Aggregates a total uptime for every single host. -Go to the to level directory and run +=item --all - make +Shows every individual uptime of all hosts. -To test run +=item --count=i - ./bin/guprecords +Show i num of entries. Default is 23. -It should print out the version number of the project. +=item --indir=s -=head2 Create a Debian package +Read all the *.records files from dir s. Default is ./ -Go to the to level directory and run +=back - make deb -It will create the files like: +=head2 Quick getting started - ../guprecords_0.0.0.0_all.deb - ../guprecords_0.0.0.0.dsc - ../guprecords_0.0.0.0_amd64.changes - ../guprecords_0.0.0.0.tar.gz +=head3 Uptimed -It should create a debian package in ../. Check and install it, e.g: +Firstival, you need to collect uprecords using the uptimed deaemon. To install it run: - lintian --pedantic ../guprecords_0.0.0.0_all.deb - sudo dpkg -i ../guprecords_0.0.0.0_all.deb + sudo aptitude install uptimed -Run +Please consult the L<uptimed> and L<uprecords> manpages. Please ensure to understand how it works and what it does. - dpkg -L guprecords +uptimed collects uprecords to -to see whats in there. You can now run + /var/spool/uptimed/records - /usr/bin/guprecords +And this file is used by guprecords for further processing. -or for example +=head3 Collect all the uprecords - man guprecords +You may have several hosts with uptimed running already. Collect all the records file to a central repository (e.g. git). Name each file FQDN.records and run -=head2 Read the Makefile + guprecords --indir ./ -Read the Makefile in order to understand what's going on. +You may automate the collecting of all the uprecords using something like cron or puppet. -=head1 Customize +=head1 LICENSE -Now, since you understood everything feel free to customize everything the way you want it. E.g.: +Se package description or project website. - Don't use POD for documentation but LaTeX +=head1 AUTHOR - Compile a C program +Paul Buetow - <http://guprecords.buetow.org> - Include a ./lib dir, add it to the 'install' Makefile rule - - etc etc. - -You should also consider the following: - -=head2 Manual page - -This template is using POD for creating manual pages. Edit ./docs/guprecords.pod and run - - make documentation - -in order to build ./docs/guprecords.1. The page will be included in the resulting debian package automatically. You can review the page with - - man ./docs/guprecords.1 - -=head2 Renaming guprecords into your project name - -Rename all files which have *guprecords* included into your own new package name. You can do that with: - - PROJECTNAME=yourproject - find . -name \*guprecords\* | - while read guprecords; do git mv $guprecords ${guprecords/guprecords/$PROJECTNAME}; done - -Search all content and rename *guprecords* into your own new package name. You can do that with: - - grep -R guprecords . | grep -v .git | - cut -d: -f1 | uniq | xargs sed -i "s/guprecords/$PROJECTNAME/g" - -=head2 Updating ./debian - -Edit the following files accordingly to your new project (e.g. with vim): - - ./debian/control - ./debian/copyright - ./debian/README - -=head2 Update changelog - -Go to the to level directory and run - - dch -i +=cut diff --git a/docs/guprecords.txt b/docs/guprecords.txt index 968b998..e8753ef 100644 --- a/docs/guprecords.txt +++ b/docs/guprecords.txt @@ -1,116 +1,56 @@ NAME - guprecords - A template project + guprecords - Global uptime records -WTF? - You may use this project in order to build your own Debian package for - your own project. This has been tested with Debian GNU/Linux Squeeze and - Debian GNU/Linux Wheezy. + Shows uprecord stats of several hosts at once. - Feel free to modify any aspects. This project is just an empty example - guprecords. + Synopsis + guprecords [--help] [--total|--all] [--count=i] [--indir=s] - Follow these steps: + Parameters + --help + Shows the help - Install required packages - Run the following: + --total + Aggregates a total uptime for every single host. - sudo aptitude install lintian devscripts dpkg-dev make perl + --all + Shows every individual uptime of all hosts. - Todo: Ensure this are the correct packages. In order to test that I - would have to setup a blank Debian system. + --count=i + Show i num of entries. Default is 23. - Compile the project - Go to the to level directory and run + --indir=s + Read all the *.records files from dir s. Default is ./ - make + Quick getting started + Uptimed + Firstival, you need to collect uprecords using the uptimed deaemon. To + install it run: - To test run + sudo aptitude install uptimed - ./bin/guprecords + Please consult the uptimed and uprecords manpages. Please ensure to + understand how it works and what it does. - It should print out the version number of the project. + uptimed collects uprecords to - Create a Debian package - Go to the to level directory and run + /var/spool/uptimed/records - make deb + And this file is used by guprecords for further processing. - It will create the files like: + Collect all the uprecords + You may have several hosts with uptimed running already. Collect all the + records file to a central repository (e.g. git). Name each file + FQDN.records and run - ../guprecords_0.0.0.0_all.deb - ../guprecords_0.0.0.0.dsc - ../guprecords_0.0.0.0_amd64.changes - ../guprecords_0.0.0.0.tar.gz + guprecords --indir ./ - It should create a debian package in ../. Check and install it, e.g: + You may automate the collecting of all the uprecords using something + like cron or puppet. - lintian --pedantic ../guprecords_0.0.0.0_all.deb - sudo dpkg -i ../guprecords_0.0.0.0_all.deb +LICENSE + Se package description or project website. - Run - - dpkg -L guprecords - - to see whats in there. You can now run - - /usr/bin/guprecords - - or for example - - man guprecords - - Read the Makefile - Read the Makefile in order to understand what's going on. - -Customize - Now, since you understood everything feel free to customize everything - the way you want it. E.g.: - - Don't use POD for documentation but LaTeX - - Compile a C program - - Include a ./lib dir, add it to the 'install' Makefile rule - - etc etc. - - You should also consider the following: - - Manual page - This template is using POD for creating manual pages. Edit - ./docs/guprecords.pod and run - - make documentation - - in order to build ./docs/guprecords.1. The page will be included in the - resulting debian package automatically. You can review the page with - - man ./docs/guprecords.1 - - Renaming guprecords into your project name - Rename all files which have *guprecords* included into your own new package - name. You can do that with: - - PROJECTNAME=yourproject - find . -name \*guprecords\* | - while read guprecords; do git mv $guprecords ${guprecords/guprecords/$PROJECTNAME}; done - - Search all content and rename *guprecords* into your own new package name. You - can do that with: - - grep -R guprecords . | grep -v .git | - cut -d: -f1 | uniq | xargs sed -i "s/guprecords/$PROJECTNAME/g" - - Updating ./debian - Edit the following files accordingly to your new project (e.g. with - vim): - - ./debian/control - ./debian/copyright - ./debian/README - - Update changelog - Go to the to level directory and run - - dch -i +AUTHOR + Paul Buetow - <http://guprecords.buetow.org> diff --git a/src/guprecords b/src/guprecords index eabe0de..1a1d81a 100755 --- a/src/guprecords +++ b/src/guprecords @@ -1,3 +1,157 @@ -#!/bin/bash +#!/usr/bin/env perl +# guprecords (c) 2014, Paul Buetow +# E-Mail: guprecords@mx.buetow.org WWW: http://guprecords.buetow.org + +use strict; +use warnings; +use v5.14; + +use Getopt::Long; +use POSIX qw(strftime); + +my $VERSION = 'VERSION_DEVEL'; +my $FLAG = 0; + +my %ARGS = ( + all => 0, + total => 0, + reverse => 0, + help => 0, + 'count=i' => 23, + 'indir=s' => '.', +); + +my %OPTS = map { $_ => \$ARGS{$_} } keys %ARGS; +GetOptions %OPTS or die "Error in command line arguments. Try --help"; + +sub help() { + say "Ths is guprecords Version $VERSION"; + print "Usage: $0\n"; + say "\t--$_" for sort keys %ARGS; + say 'Please also consult the guprecords manual page.' +} + +sub uptime($) { + my $uptime = shift; + + my ($s,$m,$h,undef,undef,$y,undef,$d) = localtime($uptime); + + $y -= 70; + $d += $y * 365; + + sprintf "%3dd %02d:%02d:%02d", $d, $h, $m, $s; +} + +sub trimlen($$) { + my ($string,$len) = @_; + + if (length $string > $len) { + substr($string, 0, ($len-2)).'..'; + } else { + $string; + } +} + +sub out(\@;$) { + my ($records,$show_bt) = @_; + + my $count = 0; + + printf "%3s | %17s | %20s | %13s | %24s\n", + 'Pos', + 'System', + 'Kernel', + 'Uptime', + (defined $show_bt ? 'Boot time' : ''); + + map { + return if $count++ == $ARGS{'count=i'}; + + printf "%3d | %17s | %20s | %13s | %24s\n", + $count, + trimlen($_->{fqdn},17), + trimlen($_->{kernel},20), + uptime($_->{uptime}), + (defined $show_bt ? ''.localtime($_->{bootime}) : ''); + } + do { + unless ($ARGS{reverse}) { + sort { $b->{uptime} <=> $a->{uptime} } @$records + } else { + sort { $a->{uptime} <=> $b->{uptime} } @$records + } + }; + + $FLAG = 1; +} + +sub all() { + my @records; + + for my $file (<$ARGS{'indir=s'}/*.records>) { + my ($fqdn) = $file =~ m#.*/(.*)\.records#; + + my $uptime_total = 0; + + open my $fh, $file or die "$file: $!\n"; + while (<$fh>) { + chomp; + my ($uptime,$boot,$kernel) = split ':'; + + push @records, { + uptime => $uptime, + bootime => $boot, + kernel => $kernel, + fqdn => $fqdn, + }; + + } + close $file; + + } + + out @records, 'show_bt'; +} + +sub total() { + my @records; + + for my $file (<$ARGS{'indir=s'}/*.records>) { + my ($fqdn) = $file =~ m#.*/(.*)\.records#; + + my $uptime_ = 0; + my $kernel_; + + my $highest = 0; + + open my $fh, $file or die "$file: $!\n"; + while (<$fh>) { + chomp; + my ($uptime,$boot,$kernel) = split ':'; + + $uptime_ += $uptime; + + if ($highest < $uptime) { + $highest = $uptime; + $kernel_ = $kernel; + } + } + close $file; + + push @records, { + uptime => $uptime_, + kernel => $kernel_, + fqdn => $fqdn, + }; + + } + + out @records; +} + +help if $ARGS{help}; +all if $ARGS{all}; +total if $ARGS{total}; + +help unless $FLAG; -echo "Hello World, I am a fake project" |
