2012년 10월 11일 목요일

L2 스위치 포트별 아이피 찾기 프로그램 (포트매핑)

L2 스위치에선 포트별 아이피를 찾을 수 없는 데, 이걸 L3장비와 연계하여 찾는 프로그램(포트매핑)을 만들어 봤다.

포항공과대학의 김은희님이 쓰신 논문이 유용한 자료가 되었다.

해당 논문에서 처럼 제대로 된 프로그램으로 만들고 싶지만, 이미 상용프로그램이 많은 상황에서 그정도의 퀄리티를 뛰어넘을 자신도 없고, 그냥 구현 해 본데에 의의를 둔다.

이미 상용으로 솔라윈즈나 OpUtils 등에 해당 기능이 포함되어 있다.

솔라윈즈의 switch port mapper 프로그램과 비교하여 보니, 정확히 비슷하진 않지만, 얼추 맞는 것으로 보인다.

문제 있을 수 있는점.
1. vlan 별로 별도 지정이 안되있어서, vlan을 추가한 L2장비에서는 어떻게 될 지 모르겠다.
2. 구글 검색하다가 보게 된건데, 시스코외의 장비에서는 조금 다른 MIB 를 사용하고 있는 것 같다.(표준MIB라고 해서 사용하기 했는 데, 잘 모르겠다.)

ps.
php 를 통해 구현했는데,  보통 호스팅으로 사용하는 서버에는 snmp 내장함수가 설치되어 있지 않다.

 

DEMO

 다운로드

2012년 10월 2일 화요일

MRTG cpu,mem,hdd,load average,active user, process,traffic 템플릿

아래의 출처에서 다운받은 템플릿.

해당 템플릿을 다운 받고 cfgmaker 에서 옵션을 --host-template=host-generic.htp 추가하여 사용하면된다.

다만 해당사이트에서 다운받은 템플릿으로 생성한 cfg 파일을 indexmaker로 만들때 아래와 같은 에러메시지가 뜬다.

ERROR: no H1 line pagetop property in 아이피-memory section

indexmaker에서 pagetop 옵션에는 H1 태그로 찾는 게 기본값인데, 해당 템플릿에서는 H1 지정이 안되어 있다.

해당 문제는 -section 옵션으로 해결할 수 있다.

아래의 옵션중에 h1 외의 옵션으로 설정하여 실행하면 된다.


 - section = h1 섹션의 헤딩으로 PageTop에 h1 태그를 사용 (기본값)
- section = title 그래프 섹션 헤딩으로 Title을 사용
- section = name 섹션 헤딩으로 그래프 이름을 사용
- section = descr 섹션 헤딩으로 그래프 description을 사용
- section = portname 섹션 헤딩으로 PageTop에 port 이름 항목을 사용


아래는 H1 태그 추가한 버젼.



# Generic host template for all systems support MIB-II host resources
# MIB. Will take care of storage devices as well, but watch out for
# changes in enumeration caused by inserting USB devices!

# Watch out for lines being wrapped by broken editing software

$head_lines .= "#---------------------------------------------------------------------\n";

if( $sysdescr =~ /linux/i ) {
$target_lines .= "routers.cgi*Icon: linux-sm.gif\n";
} elsif( $sysdescr =~ /(windows|microsoft)/i ) {
$target_lines .= "routers.cgi*Icon: windows-sm.gif\n";
} elsif( $sysdescr =~ /IBM/i ) {
$target_lines .= "routers.cgi*Icon: aix-sm.gif\n";
} elsif( $sysdescr =~ /(sun|solaris)/i ) {
$target_lines .= "routers.cgi*Icon: sun-sm.gif\n";
} elsif( $sysdescr =~ /cisco/i ) {
$target_lines .= "routers.cgi*Icon: cisco-sm.gif\n";
}
$target_lines .= <<ECHO;
routers.cgi*Description: $router_name ($sysname)
routers.cgi*ShortDesc: $sysname
#-------------------------------------------------
ECHO

my($cpuoid) = oid_pick($router_connect,$v3opt,"1.3.6.1.4.1.2021.11.11.0",
"1.3.6.1.4.1.9.2.1.58.0");
if($cpuoid) { # this uses the UCD MIB or Cisco MIB
$target_lines .= <<ECHO;
#######################################
# CPU load
Target[$router_name-cpu]: 100 - $cpuoid&$cpuoid:$router_connect
PageTop[$router_name-cpu]: <H1>$router_name CPU usage</H1>
Title[$router_name-cpu]: CPU on $router_name
Maxbytes[$router_name-cpu]: 100
Options[$router_name-cpu]: gauge
YLegend[$router_name-cpu]: percent
ShortLegend[$router_name-cpu]: %
LegendI[$router_name-cpu]: cpu:
Legend1[$router_name-cpu]: CPU usage
Legend3[$router_name-cpu]: Peak CPU usage
routers.cgi*Options[$router_name-cpu]: noo, nopercent, nototal, fixunit
routers.cgi*InOut[$router_name-cpu]: no
routers.cgi*ShortDesc[$router_name-cpu]:CPU
routers.cgi*Icon[$router_name-cpu]: chip-sm.gif
routers.cgi*InSummary[$router_name-cpu]: yes
routers.cgi*InCompact[$router_name-cpu]: yes
routers.cgi*Mode[$router_name-cpu]: cpu
routers.cgi*MBLegend[$router_name-cpu]: 100% usage
ECHO
} else { # lets try hrMIB
my (@cpus) = snmpwalk($router_connect,'1.3.6.1.2.1.25.3.3.1.2');
foreach my $tmpcpu (@cpus) {
$tmpcpu =~ /(\d+):\d/;
my( $instance )=$1;
my( $target_name )=$router_name.".cpu".$instance;
$target_lines .= <<ECHO;
Target[$target_name]: 1.3.6.1.2.1.25.3.3.1.2.$instance&1.3.6.1.2.1.25.3.3.1.2.$instance:$router_connect
Title[$target_name]: $sysname processor $instance
MaxBytes[$target_name]: 100
Options[$target_name]: gauge, unknaszero, noo
Directory[$target_name]: $directory_name
WithPeak[$target_name]: wmy
YLegend[$target_name]: % Utilisation
ShortLegend[$target_name]: %
Legend1[$target_name]: 1min CPU load on proc$instance
Legend3[$target_name]: Max 5min CPU load on proc$instance
LegendI[$target_name]: cpu$instance:
PageTop[$target_name]: <H1>$sysname Processor Load</H1>
routers.cgi*ShortDesc[$target_name]: CPU #$instance
routers.cgi*Options[$target_name]: nopercent, nototal, fixunit
routers.cgi*InOut[$target_name]: no
routers.cgi*Icon[$target_name]: chip-sm.gif
routers.cgi*InSummary[$target_name]: yes
routers.cgi*InCompact[$target_name]: yes
routers.cgi*Mode[$target_name]: cpu
routers.cgi*MBLegend[$target_name]: 100% usage
ECHO
if($#cpus > 0) {
$target_lines .= "routers.cgi*Graph[$target_name]: $router_name-allcpu average\n";
$target_lines .= "routers.cgi*InMenu[$target_name]: no\n";
}
}
if($#cpus > 0) {
$target_lines .= <<ECHO;
routers.cgi*Title[$router_name-allcpu]: All CPUs on $router_name
routers.cgi*Icon[$router_name-allcpu]: chip-sm.gif
routers.cgi*ShortDesc[$router_name-allcpu]: CPU
routers.cgi*LegendAI[$router_name-allcpu]: cpu :
ECHO
}
}

my($rmemoid) = oid_pick($router_connect,$v3opt,"1.3.6.1.4.1.2021.4.6.0");
my($smemoid) = oid_pick($router_connect,$v3opt,"1.3.6.1.4.1.2021.4.4.0");
if($rmemoid and $smemoid) { # this uses the UCD MIB
my($maxreal, $maxswap) = snmpget($router_connect,
"1.3.6.1.4.1.2021.4.5.0","1.3.6.1.4.1.2021.4.3.0");
$target_lines .= <<ECHO;
#######################################
# Memory used
Target[$router_name-memory]: $rmemoid&$smemoid:$router_connect * 1024
PageTop[$router_name-memory]: <H1>$router_name Memory Available </H1>
Title[$router_name-memory]: Available Memory on $router_name
SetEnv[$router_name-memory]: MRTG_INT_DESCR="Memory"
MaxBytes1[$router_name-memory]: $maxreal
MaxBytes2[$router_name-memory]: $maxswap
Options[$router_name-memory]: gauge
YLegend[$router_name-memory]: Bytes
ShortLegend[$router_name-memory]: b
LegendI[$router_name-memory]: real:
LegendO[$router_name-memory]: swap:
Legend1[$router_name-memory]: Available real memory
Legend2[$router_name-memory]: Available swap space
Legend3[$router_name-memory]: Peak available real
Legend4[$router_name-memory]: Peak available swap
routers.cgi*Options[$router_name-memory]: nototal
routers.cgi*Mode[$router_name-memory]: memory
routers.cgi*ShortDesc[$router_name-memory]: Memory
routers.cgi*Description[$router_name-memory]: Memory available: $router_name
routers.cgi*InOut[$router_name-memory]: no
routers.cgi*InSummary[$router_name-memory]: yes
routers.cgi*InCompact[$router_name-memory]: yes
routers.cgi*Icon[$router_name-memory]: chip-sm.gif
routers.cgi*MBLegend[$router_name-memory]: 100% usage
ECHO
}

my (@storage) = snmpwalk($router_connect,'1.3.6.1.2.1.25.2.3.1.4');
my( $fscnt ) = 0;
foreach my $tmps (@storage) {
$tmps =~ /(\d+):(\d+)/;
next if(!$2);
my($instance,$blocksize)=($1,$2);
my($diskname,$disksize,$disktype) =
snmpget($router_connect,'1.3.6.1.2.1.25.2.3.1.3.'.$instance,
'1.3.6.1.2.1.25.2.3.1.5.'.$instance, '1.3.6.1.2.1.25.2.3.1.2.'.$instance);
$disktype = $1 if($disktype =~ /\.(\d+)$/);
next if(!$diskname or !$disksize
or ($disktype!=2 and $disktype!=3 and $disktype!=4));
my($shortdisk) = $instance;
$shortdisk = $1 if($diskname =~ /^(\w):/);
my($target_name)=$router_name.".disk.".$shortdisk;
my($maxsize)= $disksize * $blocksize;

if($disktype == 2 ) { # memory use
next if($rmemoid); # we already have it
$target_lines .= <<ECHO;
#######################################
# Memory used
Target[$router_name-memory]: 1.3.6.1.2.1.25.2.3.1.6.$instance&1.3.6.1.2.1.25.2.3.1.5.$instance:$router_connect * $blocksize
PageTop[$router_name-memory]: <H1>$router_name Memory Used</H1>
Title[$router_name-memory]: Memory used on $router_name
SetEnv[$router_name-memory]: MRTG_INT_DESCR="Memory"
MaxBytes[$router_name-memory]: $maxsize
Options[$router_name-memory]: gauge
YLegend[$router_name-memory]: Bytes
ShortLegend[$router_name-memory]: b
LegendI[$router_name-memory]: used:
LegendO[$router_name-memory]: size:
Legend1[$router_name-memory]: Memory used
Legend2[$router_name-memory]: Memory total
Legend3[$router_name-memory]: Peak memory used
Legend4[$router_name-memory]: Peak memory total
routers.cgi*Options[$router_name-memory]: nototal noo
routers.cgi*Mode[$router_name-memory]: memory
routers.cgi*ShortDesc[$router_name-memory]: Memory
routers.cgi*Description[$router_name-memory]: Memory used: $router_name
routers.cgi*InOut[$router_name-memory]: no
routers.cgi*InSummary[$router_name-memory]: yes
routers.cgi*InCompact[$router_name-memory]: yes
routers.cgi*Icon[$router_name-memory]: chip-sm.gif
routers.cgi*MBLegend[$router_name-memory]: 100% usage
ECHO
} elsif($disktype == 3 ) { # swap use
next if($smemoid);
$target_lines .= <<ECHO;
#######################################
# Swap used
Target[$router_name-swap]: 1.3.6.1.2.1.25.2.3.1.6.$instance&1.3.6.1.2.1.25.2.3.1.5.$instance:$router_connect * $blocksize
PageTop[$router_name-swap]: <H1>$router_name Swap space used</H1>
Title[$router_name-swap]: Swap space used on $router_name
SetEnv[$router_name-swap]: MRTG_INT_DESCR="Swap"
MaxBytes[$router_name-swap]: $maxsize
Options[$router_name-swap]: gauge
YLegend[$router_name-swap]: Bytes
ShortLegend[$router_name-swap]: b
LegendI[$router_name-swap]: used:
LegendO[$router_name-swap]: size:
Legend1[$router_name-swap]: Swap space used
Legend2[$router_name-swap]: Swap space total
Legend3[$router_name-swap]: Peak swap used
Legend4[$router_name-swap]: Peak swap total
routers.cgi*Options[$router_name-swap]: nototal noo
routers.cgi*Mode[$router_name-swap]: memory
routers.cgi*ShortDesc[$router_name-swap]: Swap
routers.cgi*Description[$router_name-swap]: Swap space used: $router_name
routers.cgi*InOut[$router_name-swap]: no
routers.cgi*InSummary[$router_name-swap]: yes
routers.cgi*InCompact[$router_name-swap]: yes
routers.cgi*Icon[$router_name-swap]: chip-sm.gif
routers.cgi*MBLegend[$router_name-swap]: 100% usage
ECHO
} elsif($disktype == 4 ) { # filesystems
$fscnt += 1;
$target_lines .= <<ECHO;
#######################################
# Filesystem $diskname
Target[$target_name]: 1.3.6.1.2.1.25.2.3.1.6.$instance&1.3.6.1.2.1.25.2.3.1.6.$instance:$router_connect * $blocksize
PageTop[$target_name]: <H1>$router_name Disk space used ($diskname)</H1>
Title[$target_name]: Disk space used on $router_name ($diskname)
SetEnv[$target_name]: MRTG_INT_DESCR="$diskname"
MaxBytes[$target_name]: $maxsize
Options[$target_name]: gauge
YLegend[$target_name]: Bytes
ShortLegend[$target_name]: b
LegendI[$target_name]: used:
Legend1[$target_name]: Space used
Legend3[$target_name]: Peak used
routers.cgi*Options[$target_name]: nototal, noo
routers.cgi*Mode[$target_name]: general
routers.cgi*ShortDesc[$target_name]: Disk: $diskname
routers.cgi*Description[$target_name]: $router_name space used on $diskname
routers.cgi*InOut[$target_name]: no
routers.cgi*InMenu[$target_name]: yes
routers.cgi*InSummary[$target_name]: yes
routers.cgi*InCompact[$target_name]: yes
routers.cgi*Icon[$target_name]: dir-sm.gif
routers.cgi*Graph[$target_name]: $router_name-filesystems "Disk space" withtotal noo
ECHO
}
}
if($fscnt) {
$target_lines .= <<ECHO;
routers.cgi*Options[$router_name-filesystems]: available
routers.cgi*Icon[$router_name-filesystems]: dir-sm.gif
routers.cgi*ShortDesc[$router_name-filesystems]: Filesystems
routers.cgi*Title[$router_name-filesystems]: Filesystems on $router_name
ECHO
}

my($laoid) = oid_pick($router_connect,$v3opt,"1.3.6.1.4.1.2021.10.1.5.2");
my($laoid2) = oid_pick($router_connect,$v3opt,"1.3.6.1.4.1.2021.10.1.5.3");
if($laoid and $laoid2) { # this uses the UCD MIB
$target_lines .= <<ECHO;
#######################################
# Load average
Target[$router_name-lavg]: $laoid&$laoid2:$router_connect / 100
PageTop[$router_name-lavg]: <H1>$router_name Load Average</H1>
Title[$router_name-lavg]: Load Average on $router_name
SetEnv[$router_name-lavg]: MRTG_INT_DESCR="Load Average"
MaxBytes[$router_name-lavg]: 1000
Options[$router_name-lavg]: nopercent, gauge
YLegend[$router_name-lavg]: Processes
ShortLegend[$router_name-lavg]:  
LegendI[$router_name-lavg]: 5min avg:
LegendO[$router_name-lavg]: 15min avg:
Legend1[$router_name-lavg]: 5-min load average
Legend2[$router_name-lavg]: 15-min load average
Legend3[$router_name-lavg]: Peak 5-min load average
Legend4[$router_name-lavg]: Peak 15-min load average
routers.cgi*Options[$router_name-lavg]: nomax, nototal, fixunit, noo
routers.cgi*Mode[$router_name-lavg]: general
routers.cgi*ShortDesc[$router_name-lavg]: Load Avg
routers.cgi*Description[$router_name-lavg]: Load average: $router_name
routers.cgi*UnScaled[$router_name-lavg]: none
routers.cgi*WithPeak[$router_name-lavg]: none
routers.cgi*InOut[$router_name-lavg]: no
routers.cgi*InSummary[$router_name-lavg]: yes
routers.cgi*InCompact[$router_name-lavg]: no
routers.cgi*Icon[$router_name-lavg]: load-sm.gif
ECHO
}

my($ucoid) = oid_pick($router_connect,$v3opt,"1.3.6.1.2.1.25.1.5.0");
if($ucoid) {
$target_lines .= <<ECHO;
#######################################
# User count
Target[$router_name-users]: $ucoid&$ucoid:$router_connect
PageTop[$router_name-users]: <H1>$router_name Active users</H1>
Title[$router_name-users]: Active Users on $router_name
MaxBytes[$router_name-users]: 1000
SetEnv[$router_name-users]: MRTG_INT_DESCR="Users"
Options[$router_name-users]: nopercent, gauge
YLegend[$router_name-users]: Users
ShortLegend[$router_name-users]:  
LegendI[$router_name-users]: Users
Legend1[$router_name-users]: Active Users
Legend3[$router_name-users]: Peak Active Users
routers.cgi*Options[$router_name-users]: nomax, nototal, fixunit, noo
routers.cgi*Mode[$router_name-users]: general
routers.cgi*ShortDesc[$router_name-users]: Users
routers.cgi*Description[$router_name-users]: Users on $router_name
routers.cgi*UnScaled[$router_name-users]: none
routers.cgi*InOut[$router_name-users]: no
routers.cgi*InSummary[$router_name-users]: yes
routers.cgi*InCompact[$router_name-users]: no
routers.cgi*Icon[$router_name-users]: user-sm.gif
ECHO
}

my($proid) = oid_pick($router_connect,$v3opt,"1.3.6.1.2.1.25.1.6.0");
if($proid) {
my($maxpr) = snmpget($router_connect, "1.3.6.1.2.1.25.1.7.0");
$target_lines .= <<ECHO;
#######################################
# Process count
Target[$router_name-procs]: $proid&$proid:$router_connect
PageTop[$router_name-procs]: <H1>$router_name Processes</H1>
Title[$router_name-procs]: Processes on $router_name
ECHO
if($maxpr) {
$target_lines .= "MaxBytes[$router_name-procs]: $maxpr\n";
} else {
$target_lines .= "MaxBytes[$router_name-procs]: 10000\n";
}
$target_lines .=
"routers.cgi*Options[$router_name-procs]: nototal, fixunit, noo"
.($maxpr?"":", nomax")."\n";
$target_lines .= <<ECHO;
SetEnv[$router_name-procs]: MRTG_INT_DESCR="Procs"
Options[$router_name-procs]: nopercent, gauge
YLegend[$router_name-procs]: Processes
ShortLegend[$router_name-procs]:  
LegendI[$router_name-procs]: Procs
Legend1[$router_name-procs]: Processes
Legend3[$router_name-procs]: Peak Processes
routers.cgi*Mode[$router_name-procs]: general
routers.cgi*ShortDesc[$router_name-procs]: Processes
routers.cgi*Description[$router_name-procs]: Processes on $router_name
routers.cgi*UnScaled[$router_name-procs]: none
routers.cgi*InOut[$router_name-procs]: no
routers.cgi*InSummary[$router_name-procs]: yes
routers.cgi*InCompact[$router_name-procs]: no
routers.cgi*Icon[$router_name-procs]: list-sm.gif
routers.cgi*MBLegend[$router_name-procs]: Max capacity
ECHO
}



mrtg cfgmaker 팁

시스코 스위치 vlan 값 주석 처리.

cfgmaker 옵션에 '-if-filter=(!$if_is_ciscovlan)' 를 준다.

예제)

[sourcecode]
cfgmaker --global 'WorkDir:/home/mrtg/test/138' --global 'Language:korean' --global 'Options[_]: bits,growright' --output '/home/mrtg/test/cfgmaker/cfg_test4.cfg'  '-if-filter=(!$if_is_ciscovlan)'  커뮤니티@아이피
[/sourcecode]

2012년 9월 21일 금요일

qmHandler 작동 문제

qmHandler 실행시 Calling system script to terminate qmail... 부분에서 더이상 진행이 안되는 현상이 있다.

확인 결과 qmail-remote 가 qmail stop 명령어로는 정지 되지 않는다.

killall qmail-remote 후 실행하자.

2012년 9월 20일 목요일

전화번호 마스크용 스크립트

1. 전화번호 마스크용 스크립트

function MaskPhon( obj ) {

obj.value =  PhonNumStr( obj.value );

}

 

function PhonNumStr( str ){

var RegNotNum  = /[^0-9]/g;

var RegPhonNum = "";

var DataForm   = "";

 

// return blank

if( str == "" || str == null ) return "";

 

// delete not number

str = str.replace(RegNotNum,'');

if( str.length < 4 ) return str;

 

if( str.length > 3 && str.length < 7 ) {

DataForm = "$1-$2";

RegPhonNum = /([0-9]{3})([0-9]+)/;

} else if(str.length == 7 ) {

DataForm = "$1-$2";

RegPhonNum = /([0-9]{3})([0-9]{4})/;

} else if(str.length == 9 ) {

DataForm = "$1-$2-$3";

RegPhonNum = /([0-9]{2})([0-9]{3})([0-9]+)/;

} else if(str.length == 10){

if(str.substring(0,2)=="02"){

DataForm = "$1-$2-$3";

RegPhonNum = /([0-9]{2})([0-9]{4})([0-9]+)/;

}else{

DataForm = "$1-$2-$3";

RegPhonNum = /([0-9]{3})([0-9]{3})([0-9]+)/;

}

} else if(str.length > 10){

DataForm = "$1-$2-$3";

RegPhonNum = /([0-9]{3})([0-9]{4})([0-9]+)/;

}

 

while( RegPhonNum.test(str) ) {

str = str.replace(RegPhonNum, DataForm);

}

return str;

}


 


 

2. 입력박스
전화번호 : <input type='text' maxlength='13' onkeyup ='MaskPhon(this);' onkeydown='MaskPhon(this);'  >

2012년 9월 13일 목요일

Linux - Search and replace over multiple files.

예제.
<link rel="stylesheet" type="text/css" href="../includes/style.css">

위의 코드를 아래처럼 변경 하고 싶을 때.
<link rel="stylesheet" type="text/css" href="admin.css">

 

 

 
perl -pi -w -e 's/\.\.\/includes\/style\.css/admin\.css/g;' *.php

확장자가 php인 파일에서 ../includes/style.css 를 admin.css 로 모든 부분 변경.

 

Linux - Search and replace over multiple files..