パスワード生成スクリプト
[sunny256-utils.git] / pingstat
blobfa8037a87fff943b97db0d79c877af3594a2b3a4
1 #!/usr/bin/env perl
3 #=======================================================================
4 # pingstat
5 # File ID: ba10e77e-f743-11dd-adac-000475e441b9
6 # Creates ping(8) statistics for crappy connections.
8 # Character set: UTF-8
9 # ©opyleft 2005– Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later, see end of
11 # file for legal stuff.
12 #=======================================================================
14 use strict;
15 use warnings;
16 use Getopt::Long;
17 use Fcntl ':flock';
18 use File::Path;
20 $| = 1;
22 our $Debug = 0;
24 our %Opt = (
26 'debug' => 0,
27 'help' => 0,
28 'verbose' => 0,
29 'version' => 0,
33 our $progname = $0;
34 $progname =~ s/^.*\/(.*?)$/$1/;
35 our $VERSION = "0.00";
37 Getopt::Long::Configure("bundling");
38 GetOptions(
40 "debug" => \$Opt{'debug'},
41 "help|h" => \$Opt{'help'},
42 "verbose|v+" => \$Opt{'verbose'},
43 "version" => \$Opt{'version'},
45 ) || die("$progname: Option error. Use -h for help.\n");
47 $Opt{'debug'} && ($Debug = 1);
48 $Opt{'help'} && usage(0);
49 if ($Opt{'version'}) {
50 print_version();
51 exit(0);
54 my $Host = "194.248.216.19";
55 my $log_dir = "$ENV{HOME}/log/pingstat";
56 my $log_file = "$log_dir/pingstat.log";
57 my $lock_dir = "/var/lock/pingstat.LOCK";
58 my $CMD_PING = "/bin/ping -c 1 $Host";
59 my $Delay = 30;
61 (-d $log_dir) || mkpath($log_dir) || die("$progname: $log_dir: Cannot create directory: $!\n");
63 my $last_time = 0;
65 mkdir($lock_dir) || die("$lock_dir: Lockdir already exists.\n");
67 while (1) {
68 my $curr_time = time;
69 my ($Sec, $Min, $Hour, $Day, $Mon, $Year, $Wday, $Yday, $is_dst) =
70 gmtime($curr_time);
71 my $Date = sprintf("%04u-%02u-%02uT%02u:%02u:%02uZ",
72 $Year + 1900, $Mon + 1, $Day, $Hour, $Min, $Sec);
73 $Year += 1900; # Weird piece of shit
74 $Mon += 1; # Urgh
75 if (open(PipeFP, "$CMD_PING |")) {
76 my $Data = join("", <PipeFP>);
77 close(PipeFP);
78 $Data =~ s/\n/\t/gs;
79 if (open(LogFP, ">>", $log_file)) {
80 if (flock(LogFP, LOCK_EX)) {
81 print(LogFP "$Date\t$Data\n");
82 close(LogFP);
83 } else {
84 warn("$progname: $log_file: Cannot lock file: $!\n");
86 } else {
87 warn("$progname: $log_file: Cannot create or append to file: $!\n");
89 } else {
90 warn("$progname: \"$CMD_PING\": Could not open pipe: $!\n");
92 while (time < ($curr_time + $Delay)) {
93 # NOP
97 sub print_version {
98 # Print program version {{{
99 print("$progname v$VERSION\n");
100 # }}}
101 } # print_version()
103 sub usage {
104 # Send the help message to stdout {{{
105 my $Retval = shift;
107 if ($Opt{'verbose'}) {
108 print("\n");
109 print_version();
111 print(<<END);
113 Usage: $progname [options] [file [files [...]]]
115 Options:
117 -h, --help
118 Show this help.
119 -v, --verbose
120 Increase level of verbosity. Can be repeated.
121 --version
122 Print version information.
123 --debug
124 Print debugging messages.
127 exit($Retval);
128 # }}}
129 } # usage()
131 sub msg {
132 # Print a status message to stderr based on verbosity level {{{
133 my ($verbose_level, $Txt) = @_;
135 if ($Opt{'verbose'} >= $verbose_level) {
136 print(STDERR "$progname: $Txt\n");
138 # }}}
139 } # msg()
141 sub D {
142 # Print a debugging message {{{
143 $Debug || return;
144 my @call_info = caller;
145 chomp(my $Txt = shift);
146 my $File = $call_info[1];
147 $File =~ s#\\#/#g;
148 $File =~ s#^.*/(.*?)$#$1#;
149 print(STDERR "$File:$call_info[2] $$ $Txt\n");
150 return("");
151 # }}}
152 } # D()
154 __END__
156 # Plain Old Documentation (POD) {{{
158 =pod
160 =head1 NAME
164 =head1 SYNOPSIS
166 [options] [file [files [...]]]
168 =head1 DESCRIPTION
172 =head1 OPTIONS
174 =over 4
176 =item B<-h>, B<--help>
178 Print a brief help summary.
180 =item B<-v>, B<--verbose>
182 Increase level of verbosity. Can be repeated.
184 =item B<--version>
186 Print version information.
188 =item B<--debug>
190 Print debugging messages.
192 =back
194 =head1 BUGS
198 =head1 AUTHOR
200 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
202 =head1 COPYRIGHT
204 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
205 This is free software; see the file F<COPYING> for legalese stuff.
207 =head1 LICENCE
209 This program is free software: you can redistribute it and/or modify it
210 under the terms of the GNU General Public License as published by the
211 Free Software Foundation, either version 2 of the License, or (at your
212 option) any later version.
214 This program is distributed in the hope that it will be useful, but
215 WITHOUT ANY WARRANTY; without even the implied warranty of
216 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
217 See the GNU General Public License for more details.
219 You should have received a copy of the GNU General Public License along
220 with this program.
221 If not, see L<http://www.gnu.org/licenses/>.
223 =head1 SEE ALSO
225 =cut
227 # }}}
229 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :