📄 gnugk_wrapper.pl
字号:
#!/usr/bin/perl#### Wrapper for gnugk (openh323gk)# Forks and executes gnguk in one fork branch and restarts gnugk proccess if it terminated.# Other fork branch monitors gnugk status port and kills it if gnugk frozen or smth.# Usage: ./gnugk_wrapper.pl [gnugk options]# This script uses gnugk options to extract path to config file and home IP address.# It extracts Home IP address and status port number from config file.# -i or --interface option overrides home IP address in config file# Default Home IP address is 127.0.0.1, and status port is 7000# you should take a look to some configuration variables below.# (c) Data Tech Labs###### CVS# RCS filename: $RCSfile: gnugk_wrapper.pl,v $# Author: $Author: willamowius $# File version: $Revision: 1.1 $# Last changes (GMT): $Date: 2005/07/20 22:57:58 $# Branch (or tag): $Name: v2_2_3 $#### modulesuse strict;use Time::localtime;use Net::Telnet;### Configure options #### path to gnugk executeablemy $gnugk = "/usr/local/bin/gnugk";#my $gnugk = "/usr/home/valdiic/dev/gnugk_utils/gnugk_status_port_simulator.pl";# set to 1 if you want to see additonal debug messages# set to 0 to disable any kind of outputmy $debug = 1;# set to 1 to logmy $logGnugkRestarts = 1;# set to 1 to run this script as a daemon (in background)# set to 0 to run this script in foregroundmy $runInBackground = 1;# set to 1 to check status portmy $checkStatusPort = 1;#gnugk telnet (status) port checking interval in secondsmy $statusPortCheckInterval = 60;# socket timeout in secondsmy $socketTimeout = 10;# set to 1 to check maximum number of blank lines; set to 0 to disable checkingmy $sockCheckMaxBlankLines = 1;# maximum number of blank (contain non-usable chracters only) lines# that is allowed from gnugk status port during process of# getting long (ends with semicolon (;)) message from status portmy $sockMaxBlankLines = 100;# script pid file directorymy $pidFileDir = "/usr/local/var/run";# gnugk pid file directorymy $gnugkPidFileDir = "/usr/local/var/run";# set to 1 to enable copying core file to core file directorymy $copyCore = 1;# core file directorymy $coreFileDirectory = "/usr/local/var/crash";# log file directorymy $logFileDir = "/usr/local/var/log";# path to log filemy $logFile = "$logFileDir/gnugk_wrapper.log";### DO NOT EDIT ANYTHING BELOW THIS LINE!!! #### print script usage# input: none# output: nonesub usage{ my $str = <<EEE;Gnugk (openh323gk) wrapper.Usage: execute gnugk wrapper with all neccessary gnugk options: ./gnugk_wrapper [gnugk_options] print this help: ./gnugk_wrapper -h ./gnugk_wrapper --helpEEE print $str;};#gets paramaters from STDIN#input: (array) cmd line arguments#output: (hash) configuration valuessub getConfStdin{ my @input = @{$_[0]}; my $i = @input; # num of items in ARGV my $j = 0; my %conf = ( 'path_to_gnugk_conf_file' => '', 'home_ip_address' => '', 'main_section_name' => '[Gatekeeper::Main]', ); #set parameters for ($j = 0; $j < $i; $j++) { # get path to gnugk config file from gnugk options if (($input[$j] eq '-c' || $input[$j] eq '--config') && $input[$j+1] ne '') { $conf{'path_to_gnugk_conf_file'} = $input[$j+1]; } # get path to gnugk config file from gnugk options elsif (($input[$j] eq '-i' || $input[$j] eq '--interface') && $input[$j+1] ne '') { $conf{'home_ip_address'} = $input[$j+1]; } # get main config file section name elsif (($input[$j] eq '-s' || $input[$j] eq '--section') && $input[$j+1] ne '') { $conf{'main_section_name'} = $input[$j+1]; } #print usage (help) elsif ($input[$j] eq '-h' || $input[$j] eq '--help') { usage(); exit(0); }; }; return %conf;};# get gnugk home ip address from gnugk config file# gnugk has to be attached to some kind of IP address# input: (string) path to gnugk config file, (string) home IP address from stdin# output: (string) Home IP address, (int) status port numbersub getConfFromFile{ my $pathToConfFile = shift; my $homeIpAddressStdin = shift; my $mainSectionName = shift; my $defaultHomeIpAddress = "127.0.0.1"; my $defaultStatusPort = "7000"; my $homeIpAddress = $defaultHomeIpAddress; my $statusPort = $defaultStatusPort; print "---\n\n" if ($debug); print "Searching for gnugk home IP address and status port\n" if ($debug); print " Path to config file: $pathToConfFile\n Main section name: $mainSectionName\n" if ($debug); print " Default home IP address and status port: $defaultHomeIpAddress:$defaultStatusPort\n" if ($debug); # return default values if no config file path given if ($pathToConfFile eq '') { print " No config file path received. Returning default values...\n" if ($debug); return ($homeIpAddress, $statusPort); }; if ($mainSectionName eq '') { print " No config file main section name received. Returning default values...\n" if ($debug); return ($homeIpAddress, $statusPort); }; # open file eval { open (CF, "<$pathToConfFile") or die "Error: can not open gnugk config file $pathToConfFile\n $!"; }; if ($@) { print $@ if ($debug); return ($homeIpAddress, $statusPort); }; # read all from file in array my @file = <CF>; # close file close(CF); # search for IP address my $row = ''; my $i = 0; my $numRows = @file; my $sectionGatekeeperMain = 0; # shows if config file section Gatekeeper::Main is found for ($i = 0; $i < $numRows; $i++) { $row = $file[$i]; $row = (split(/#|;/, $row))[0]; #read row from file, cut out comments $row =~ s/ |\n|\t//g; # cut out whitespaces, tabs, newlines #search for IP address definition if (length($row) > 0) { # find Gatekeeper::Main section in config file if ($row =~ m/\Q$mainSectionName\E/) { print " Found section [Gatekeeper::Main] in line $i\n" if ($debug); $sectionGatekeeperMain = 1; } # set section Gatekeeper::Main identificator to 0 if other section found elsif ($row =~ m/\[[A-Z_a-z]+[:]{0,2}[A-Z_a-z]+]/) { #print " Found section $row in line $i\n" if ($debug); $sectionGatekeeperMain = 0; } # set home IP address elsif ($row =~ m/Home=/ && $sectionGatekeeperMain) { print " Home IP address found in line $i\n" if ($debug); $homeIpAddress = (split("=", $row))[1]; } # set status port elsif ($row =~ m/StatusPort=/ && $sectionGatekeeperMain) { print " Status port number found in line $i\n" if ($debug); $statusPort = (split("=", $row))[1]; }; }; }; # replace detected home IP with the one from cmd line if nececssary if ($homeIpAddressStdin) { print " Replacing home IP from config file ($homeIpAddress)\n" if ($debug); print " with the one supplied from cmd line ($homeIpAddressStdin)\n" if ($debug); $homeIpAddress = $homeIpAddressStdin; }; # check IP address against format if (!checkIpv4Address($homeIpAddress)) { print " Home IP address format invalid - replacing with default value\n" if ($debug); $homeIpAddress = $defaultHomeIpAddress; }; # check port number aginst format $statusPort = int($statusPort); $statusPort = $defaultStatusPort if ($statusPort <= 0); print " Gnugk home IP and port found: $homeIpAddress:$statusPort\n" if ($debug); return ($homeIpAddress, $statusPort);};# checks IP V4 address format# input: (string) IP V4 address# output: (int) 1 - valid; 0 - invalidsub checkIpv4Address{ my $ip = shift; my $ret = 0; # check if there are four octets with numbers if ($ip =~ m/([0-9]{1,3}\.){3}[0-9]{1,3}/) { my @numbers = split('\.', $ip); my $numElements = @numbers; my $i = 0; for ($i = 0 ; $i < $numElements; $i++) { $numbers[$i] = int($numbers[$i]); # generally all numbers in all octets must be >0 and <255 if ($numbers[$i] < 0 || $numbers[$i] >= 255) { $ret = 0; last; } # number in first octet must be >0 elsif ($numbers[$i] <= 0 && $i == 0) { $ret = 0; last; } # everything ok else { $ret = 1; }; }; } else { $ret = 0; }; return $ret;};# write pid to file# input: (string) path to pid file, (int) pid# output: (none)sub writePidFile{ my $pidFile = shift; my $pid = shift; return 0 if (!$pidFile); # open file for appending by default my $openMode = ">>"; # open for rewriting if initial open $openMode = ">" if (!$pid); # open file eval { open (GKPID, "$openMode$pidFile") or die "Error: can not open gnugk wrapper pid file $pidFile\n $!"; }; if ($@) { print $@ if ($debug); return 0; }; # write pid if ($pid) { print GKPID "$pid\n"; }; close (GKPID); return 1;};# get gnugk pid from process list# input: (string) path to gnugk pid file# output: (int) gnugk pid (default: 0 if unsuccessful)sub getGnugkPid{ my $gnugkPidFile = shift; my $gnugkPid = 0; return $gnugkPid if ($gnugkPidFile eq ''); print " Searching for gnugk pid in file: $gnugkPidFile\n" if ($debug); # read file eval { open (PIDFILE, "<$gnugkPidFile") or die "Error: can not open gnugk pid file $gnugkPidFile\n $!"; }; if ($@) { print $@ if ($debug); return $gnugkPid; }; my @file = <PIDFILE>; close (PIDFILE); # extract first appearing positive integer from file foreach (@file) { $_ = int ($_); # get integer value from line # set gnugk pid to $_ integer value and break loop if ($_ > 0) { $gnugkPid = $_; last; }; }; print " Gnugk pid detected: $gnugkPid\n" if ($debug); return $gnugkPid;};# kill gnugk process# input: (int) gnugk pid
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -