これでいけるかな?
★送り側★
use CGI;
use CGI::Session qw/-ip-match/;
use CGI::Carp qw(fatalsToBrowser);
my $cgi = new CGI;
# CGI::Sessionnオブジェクトの生成
my $session = new CGI::Session(undef, $cgi, {Directory=>'./session'});
$session->param("user_id", "nitc");
$session->expire('+1m');
print $session->header(-charset=>'euc-jp');
print $session->id();
★受け側★
use CGI;
use CGI::Session;
use CGI::Carp qw(fatalsToBrowser);
my $cgi = new CGI;
# CGI::Sessionnオブジェクトの生成
my $session = CGI::Session->load(undef, $cgi, {Directory=>'./session'});
my $val=$session->param("user_id");
print $session->header(-charset=>'euc-jp');
print "$val,";
$session_id = $session->id();
print "$session_id, ";
if ($session->is_empty ) {
print "empty\n";
}
if ($session->is_expired ) {
print "expired\n";
}
if (!defined($val)) {
print "no session\n";
}
2010年2月21日日曜日
2010年2月16日火曜日
CGI-Session-4.42.tar.gzのためにいれたもの
CGI.pm-3.49.tar.gz
FCGI-0.68.tar.gz
Test-Simple-0.94.tar.gz
★おくりて
# 有効なセッションIDを取得
$CGISESSID = $session->id();
# セッションにデータを格納
$session->param('user_id', $user_id);
# or
#$session->param(-name=>'l_name', -value=>'Ruzmetov');
# データの回収
#my $f_name = $session->param('f_name');
# または
#my $l_name = $session->param(-name=>'l_name');
# 特定セッションパラメータのクリア
#$session->clear(["_IS_LOGGED_IN"]);
# '_IS_LOGGED_IN'フラグは10分間アイドルした後期限切れになります
#$session->expire(_IS_LOGGED_IN => '+10m');
# セッションそのものが1minutesアイドルした後期限切れになります
$session->expire('+1m');
# セッションを削除したほうが良いでしょう
#$session->delete();
$querry->header(
-cookie=>$querry->cookie(-name=>'CGISESSID',
-value=>$CGISESSID)
);
#print $querry->redirect("/cgi-bin/receiveSession.pl?CGISESSID=$CGISESSID");
★うけて
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Session qw/-ip_match/;
my $cgi=CGI->new;
my $sid=$cgi->cookie('CGISESSID')||$cgi->param('CGISESSID')||undef;
#1.cookieからCGISESSIDを探す
#2.cookieから取れなかったらurlパラメータを探す.
#3.どちらも取得できなかったらundef.
my $session=CGI::Session->new(undef,$sid,{Directory=>'./tmp'});
#4.取得したセッションidが有効ならそのまま.無効なら別のidを発番.
print $cgi->header(-charset=>'euc-jp'),
$cgi->start_html(-lang=>'ja',
-encoding=>'euc-jp',
-title=>'CGI.pm使用/遷移先');
if(defined $sid && $sid eq $session->id){
#cookieかurlパラメータから値を取得でき,かつ有効なid
print $cgi->p('セッションは有効
',
'session id: '.$session->id.'
',
'user_id: '.$session->param('user_id'));
}
elsif(defined $sid && $sid ne $session->id){
#cookie,またはurlパラメータから値を取得できた.しかしidとしては無効
print $cgi->p('セッションは無効
',
'$sid: '.$sid.'
',
'$session->id: '.$session->id);
#不要なidはさっさと消去
#先にcloseをしないと,deleteで
#'(in cleanup) could not flush: Couldn't unlink .session/cgisess_CGISESSID'
#が発生する.エラーが出てもファイルは消える.
#closeは遅いらしい
$session->close;
$session->delete;
}
else{
#cookie,またはurlパラメータから値を取得できない.
print $cgi->p('セッションなし
',
'$sid: undefined'),
}
print $cgi->end_html;
#end
FCGI-0.68.tar.gz
Test-Simple-0.94.tar.gz
★おくりて
# 有効なセッションIDを取得
$CGISESSID = $session->id();
# セッションにデータを格納
$session->param('user_id', $user_id);
# or
#$session->param(-name=>'l_name', -value=>'Ruzmetov');
# データの回収
#my $f_name = $session->param('f_name');
# または
#my $l_name = $session->param(-name=>'l_name');
# 特定セッションパラメータのクリア
#$session->clear(["_IS_LOGGED_IN"]);
# '_IS_LOGGED_IN'フラグは10分間アイドルした後期限切れになります
#$session->expire(_IS_LOGGED_IN => '+10m');
# セッションそのものが1minutesアイドルした後期限切れになります
$session->expire('+1m');
# セッションを削除したほうが良いでしょう
#$session->delete();
$querry->header(
-cookie=>$querry->cookie(-name=>'CGISESSID',
-value=>$CGISESSID)
);
#print $querry->redirect("/cgi-bin/receiveSession.pl?CGISESSID=$CGISESSID");
★うけて
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Session qw/-ip_match/;
my $cgi=CGI->new;
my $sid=$cgi->cookie('CGISESSID')||$cgi->param('CGISESSID')||undef;
#1.cookieからCGISESSIDを探す
#2.cookieから取れなかったらurlパラメータを探す.
#3.どちらも取得できなかったらundef.
my $session=CGI::Session->new(undef,$sid,{Directory=>'./tmp'});
#4.取得したセッションidが有効ならそのまま.無効なら別のidを発番.
print $cgi->header(-charset=>'euc-jp'),
$cgi->start_html(-lang=>'ja',
-encoding=>'euc-jp',
-title=>'CGI.pm使用/遷移先');
if(defined $sid && $sid eq $session->id){
#cookieかurlパラメータから値を取得でき,かつ有効なid
print $cgi->p('セッションは有効
',
'session id: '.$session->id.'
',
'user_id: '.$session->param('user_id'));
}
elsif(defined $sid && $sid ne $session->id){
#cookie,またはurlパラメータから値を取得できた.しかしidとしては無効
print $cgi->p('セッションは無効
',
'$sid: '.$sid.'
',
'$session->id: '.$session->id);
#不要なidはさっさと消去
#先にcloseをしないと,deleteで
#'(in cleanup) could not flush: Couldn't unlink .session/cgisess_CGISESSID'
#が発生する.エラーが出てもファイルは消える.
#closeは遅いらしい
$session->close;
$session->delete;
}
else{
#cookie,またはurlパラメータから値を取得できない.
print $cgi->p('セッションなし
',
'$sid: undefined'),
}
print $cgi->end_html;
#end
2010年2月10日水曜日
perl tips
$querry = new CGI;
$user_id = $querry->param("user_id");
$user_pass = $querry->param("user_pass");
$dbh = DBI->connect("DBI:Pg:dbname=$dbname", "$dbusername", "$dbpassword");
$sql = "select count(*) from auth_tbl where user_id = ? and user_password = ?";
$sth = $dbh->prepare($sql) or die "can not execute: ". $sth->errstr();
$sth->execute($user_id, $user_pass) or die "Cannot execute: ".$sth->errstr();
@row;
@row = $sth->fetchrow_array();
$cnt = $row[0];
if ($cnt == 1) {
$auth_flg = "true";
}
$sth->finish();
if( $auth_flg ne "true") {
print $querry->redirect("/aaa.html");
} else {
print $querry->redirect("/bbb.cgi");
}
$user_id = $querry->param("user_id");
$user_pass = $querry->param("user_pass");
$dbh = DBI->connect("DBI:Pg:dbname=$dbname", "$dbusername", "$dbpassword");
$sql = "select count(*) from auth_tbl where user_id = ? and user_password = ?";
$sth = $dbh->prepare($sql) or die "can not execute: ". $sth->errstr();
$sth->execute($user_id, $user_pass) or die "Cannot execute: ".$sth->errstr();
@row;
@row = $sth->fetchrow_array();
$cnt = $row[0];
if ($cnt == 1) {
$auth_flg = "true";
}
$sth->finish();
if( $auth_flg ne "true") {
print $querry->redirect("/aaa.html");
} else {
print $querry->redirect("/bbb.cgi");
}
2010年2月9日火曜日
覚書 あぱっちとぽすぐれなど
★Webサーバの構築
centOS5.0にはApache2.2が標準で採用されている
①インストール
yum -y install httpd
②マニュアルもインストール
yum -y install httpd -manual ???? なぜかエラー
※yumをつかってインストールしたときのディレクトリ構成
http://www.atmarkit.co.jp/flinux/rensai/apache2_01/apache01b.html
/etc/httpd/conf/ httpd.confなど設定ファイル
/usr/lib/httpd/build/ 拡張モジュール作成時に使用
/usr/lib/httpd/modules/ 拡張モジュール
/usr/sbin/ apachectl、httpdなどの実行ファイル
/usr/share/man/ マニュアル(man形式)
/var/log/httpd/ ログファイル
/var/www/
├─ cgi-bin/ CGIスクリプト
├─ error/ エラーメッセージファイル
├─ html/ Webサイトのドキュメントルート
├─ icons/ アイコンファイル
└─ manual/ マニュアル(HTML形式)
③Apacheの設定
/etc/httpd/conf/httpd.conf
・ServerNameの追加
・AddDefaultCharset UTF-8をコメントアウト
④Apacheの起動と停止
/etc/init.d/http -k start
/etc/init.d/http -k stop
⑤Apacheno自動起動設定
chkconfig httpd on
★postgreSQLのインストール
①インストール
yum install postgresql-server
※インストールの確認
rpm -qa | grep postgres
postgresql-libs-8.1.4-1.1
postgresql-8.1.18-2.el5_4.1
postgresql-server-8.1.18-2.el5_4.1
※ユーザーも作られている
id postgres
uid=26(postgres) gid=26(postgres) 所属グループ=26(postgres)
※パスワードの設定
rootでpasswd postgres
②サービスの登録
chkconfig postgresql on
※確認
chkconfig --list postgresql
④起動
/etc/init.d/postgresql start
⑤psql接続 ・・・以下postgre
※データベースの一覧を表示
psql -l
List of databases
Name | Owner | Encoding
-----------+----------+----------
postgres | postgres | EUC_JP
template0 | postgres | EUC_JP
template1 | postgres | EUC_JP
(3 rows)
※新規ユーザーの作成 --> nitc (デフォルトの設定ではOSユーザーと一致している必要がある?)
createuser nitc
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y
CREATE ROLE
※2009/02/02・・・CGIから利用するため、OS認証→パスワード認証に変更
※
※1)設定ファイル変更 /var/lib/pgsql/data/pg_hba.conf もともとはident sameuser
※ local all all password
※
※2)サービス再起動
※/etc/init.d/postgresql restart
※rootにて
※
※3)パスワードの設定
※ALTER ROLE nitc with password 'xxx';
※ALTER ROLE postgres with password 'xxx';
⑥データベースの作成
※先ほど作成したデータベースユーザーとLinuxのユーザーは一致している必要がある
su -nitc
$ createdb hogedb
CREATE DATABASE
$ psql -l
List of databases
Name | Owner | Encoding
-----------+----------+----------
hogedb | nitc | EUC_JP
postgres | postgres | EUC_JP
template0 | postgres | EUC_JP
template1 | postgres | EUC_JP
(4 rows)
★postgresqlのトランザクション管理
・明示的にトランザクションを開始しないと、自動でcommitされる
BEGIN ENDまたはCOMMIT ROLLBACK
★FTPでASCIIモードで転送しても、改行コードが自動で変更されない
http://www.aconus.com/~oyaji/ftp/vsftpd_rpm.htm
→ linux 側のFTPデーモンの設定が、デフォルトでasciiモード不可になっていたため。
→ /etc/vsftpd/vsftpd.confを編集
ascii_download_enable=YES (デフォルト:NO)
ascii_upload_enable=YES (デフォルト:NO)
>>ASCⅡモードでのダウンロード・アップロードが何故かデフォルトで不可になっている。 YES にしないと、CGI が動かないという痛い目にあう。
→デーモン再起動
/sbin/service vsftpd restart
centOS5.0にはApache2.2が標準で採用されている
①インストール
yum -y install httpd
②マニュアルもインストール
yum -y install httpd -manual ???? なぜかエラー
※yumをつかってインストールしたときのディレクトリ構成
http://www.atmarkit.co.jp/flinux/rensai/apache2_01/apache01b.html
/etc/httpd/conf/ httpd.confなど設定ファイル
/usr/lib/httpd/build/ 拡張モジュール作成時に使用
/usr/lib/httpd/modules/ 拡張モジュール
/usr/sbin/ apachectl、httpdなどの実行ファイル
/usr/share/man/ マニュアル(man形式)
/var/log/httpd/ ログファイル
/var/www/
├─ cgi-bin/ CGIスクリプト
├─ error/ エラーメッセージファイル
├─ html/ Webサイトのドキュメントルート
├─ icons/ アイコンファイル
└─ manual/ マニュアル(HTML形式)
③Apacheの設定
/etc/httpd/conf/httpd.conf
・ServerNameの追加
・AddDefaultCharset UTF-8をコメントアウト
④Apacheの起動と停止
/etc/init.d/http -k start
/etc/init.d/http -k stop
⑤Apacheno自動起動設定
chkconfig httpd on
★postgreSQLのインストール
①インストール
yum install postgresql-server
※インストールの確認
rpm -qa | grep postgres
postgresql-libs-8.1.4-1.1
postgresql-8.1.18-2.el5_4.1
postgresql-server-8.1.18-2.el5_4.1
※ユーザーも作られている
id postgres
uid=26(postgres) gid=26(postgres) 所属グループ=26(postgres)
※パスワードの設定
rootでpasswd postgres
②サービスの登録
chkconfig postgresql on
※確認
chkconfig --list postgresql
④起動
/etc/init.d/postgresql start
⑤psql接続 ・・・以下postgre
※データベースの一覧を表示
psql -l
List of databases
Name | Owner | Encoding
-----------+----------+----------
postgres | postgres | EUC_JP
template0 | postgres | EUC_JP
template1 | postgres | EUC_JP
(3 rows)
※新規ユーザーの作成 --> nitc (デフォルトの設定ではOSユーザーと一致している必要がある?)
createuser nitc
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y
CREATE ROLE
※2009/02/02・・・CGIから利用するため、OS認証→パスワード認証に変更
※
※1)設定ファイル変更 /var/lib/pgsql/data/pg_hba.conf もともとはident sameuser
※ local all all password
※
※2)サービス再起動
※/etc/init.d/postgresql restart
※rootにて
※
※3)パスワードの設定
※ALTER ROLE nitc with password 'xxx';
※ALTER ROLE postgres with password 'xxx';
⑥データベースの作成
※先ほど作成したデータベースユーザーとLinuxのユーザーは一致している必要がある
su -nitc
$ createdb hogedb
CREATE DATABASE
$ psql -l
List of databases
Name | Owner | Encoding
-----------+----------+----------
hogedb | nitc | EUC_JP
postgres | postgres | EUC_JP
template0 | postgres | EUC_JP
template1 | postgres | EUC_JP
(4 rows)
★postgresqlのトランザクション管理
・明示的にトランザクションを開始しないと、自動でcommitされる
BEGIN ENDまたはCOMMIT ROLLBACK
★FTPでASCIIモードで転送しても、改行コードが自動で変更されない
http://www.aconus.com/~oyaji/ftp/vsftpd_rpm.htm
→ linux 側のFTPデーモンの設定が、デフォルトでasciiモード不可になっていたため。
→ /etc/vsftpd/vsftpd.confを編集
ascii_download_enable=YES (デフォルト:NO)
ascii_upload_enable=YES (デフォルト:NO)
>>ASCⅡモードでのダウンロード・アップロードが何故かデフォルトで不可になっている。 YES にしないと、CGI が動かないという痛い目にあう。
→デーモン再起動
/sbin/service vsftpd restart
覚書 PERLでのDBアクセスについて
★DBI
①CPANからダウンロードしてインストール
CPAN : http://search.cpan.org
DBI : DBI-1.609.tar.gz
DBD::PG : DBD-Pg-2.16.1.tar.gz
インストールはそれぞれ以下のように行う。
$ tar zxvf DBI-1.609.tar.gz
$ cd DBI-1.609
$ perl Makefile.PL
$ make
$ su
# make install
※gcc がはいっていないため、make でエラー
→ yum install gcc
再度make
$ tar zxvf DBD-Pg-2.16.1.tar.gz
$ cd DBD-Pg-1.49
$ perl Makefile.PL
$ make
$ su
# make install
※perl Makefile.PLでpg_config のパスを聞かれるがない・・・
→yum install postgresql-devel
http://www13.big.or.jp/~rinken/kaze/arc/200703.html
use DBI;
$dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$host;port=$port;" .
"options=$options;tty=$tty", "$username", "$password");
※makeでエラー
→version-0.80.tar.gzをインストールして、再度DBI、DBDをインストールして回避
http://groups.google.co.jp/group/spreadsheet-writeexcel/browse_thread/thread/4ecf8ae7fbe4f656?pli=1
★CGIからのDB接続
・DB接続をパスワード接続に変更
centOS.txt参照
・
★エラートラップ(CGI::Carp)の利用
★テストスクリプト
#!/usr/bin/perl
#
# DBI のテスト
# 検索条件を指定してPostgreSQLにアクセスする
#
use DBI;
$dbname = "hoge";
$dbusername="name";
$dbpassword="passwd";
$sql="";
$user_id = "userid";
$dbh = DBI->connect("DBI:Pg:dbname=$dbname", $dbusername, $dbpassword);
$sql = "select count(*) from auth_tbl where user_id = ?";
#print $sql;
$sth = $dbh->prepare($sql) or die "can not execute: ". $sth->errstr();
$sth->execute($user_id) or die "Cannot execute: ".$sth->errstr();
@row;
#while (@row = $sth->fetchrow_array()) {
# print join(",", @row). "\n";
#}
@row = $sth->fetchrow_array();
print "count[".@row."]";
if ($row[0] >= 1) {
print "あったよー";
} else {
print "ない・・・";
}
$sth->finish();
①CPANからダウンロードしてインストール
CPAN : http://search.cpan.org
DBI : DBI-1.609.tar.gz
DBD::PG : DBD-Pg-2.16.1.tar.gz
インストールはそれぞれ以下のように行う。
$ tar zxvf DBI-1.609.tar.gz
$ cd DBI-1.609
$ perl Makefile.PL
$ make
$ su
# make install
※gcc がはいっていないため、make でエラー
→ yum install gcc
再度make
$ tar zxvf DBD-Pg-2.16.1.tar.gz
$ cd DBD-Pg-1.49
$ perl Makefile.PL
$ make
$ su
# make install
※perl Makefile.PLでpg_config のパスを聞かれるがない・・・
→yum install postgresql-devel
http://www13.big.or.jp/~rinken/kaze/arc/200703.html
use DBI;
$dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$host;port=$port;" .
"options=$options;tty=$tty", "$username", "$password");
※makeでエラー
→version-0.80.tar.gzをインストールして、再度DBI、DBDをインストールして回避
http://groups.google.co.jp/group/spreadsheet-writeexcel/browse_thread/thread/4ecf8ae7fbe4f656?pli=1
★CGIからのDB接続
・DB接続をパスワード接続に変更
centOS.txt参照
・
★エラートラップ(CGI::Carp)の利用
★テストスクリプト
#!/usr/bin/perl
#
# DBI のテスト
# 検索条件を指定してPostgreSQLにアクセスする
#
use DBI;
$dbname = "hoge";
$dbusername="name";
$dbpassword="passwd";
$sql="";
$user_id = "userid";
$dbh = DBI->connect("DBI:Pg:dbname=$dbname", $dbusername, $dbpassword);
$sql = "select count(*) from auth_tbl where user_id = ?";
#print $sql;
$sth = $dbh->prepare($sql) or die "can not execute: ". $sth->errstr();
$sth->execute($user_id) or die "Cannot execute: ".$sth->errstr();
@row;
#while (@row = $sth->fetchrow_array()) {
# print join(",", @row). "\n";
#}
@row = $sth->fetchrow_array();
print "count[".@row."]";
if ($row[0] >= 1) {
print "あったよー";
} else {
print "ない・・・";
}
$sth->finish();
2010年2月8日月曜日
覚書 PERLでのSNMP利用について(net-snmp利用)
★まずはお勉強
●SNMPの大きな機能
①情報の要求と応答(get-request)
マネージャからエージェントに監視対象機器の情報を要求します。
エージェントは要求された情報を取得してマネージャに応答します。
②情報の設定要求と応答(set-request)
マネージャからエージェントに監視対象機器の設定変更を要求します。
エージェントは要求された設定変更を実行し、結果をマネージャに応答します。
③状態変化の通知(Trap)
※今回は、get-requestが実装できればいいわなと
★やってみる
①net-snmpのインストール
yum -y install net-snmp
※落とす場合はここから
http://www.net-snmp.org/
net-snmp-5.5.tar.gz
②SNMPDデーモンの起動
③/usr/src/net-snmp-5.5/perl/SNMP/examples
を実行しようとしたら、SNMP.pmがないとのことなので
※参考http://hata.cc/msql/index.htm
②CPANからSNMPを導入
SNMP-5.0401.tar.gz
こちらのMAKEでもmake: net-snmp-config: コマンドが見つかりませんでしたがでるので
③yum install net-snmp-devel
※参考:http://d.hatena.ne.jp/taslam/20080422/1208834598
④もういっかい
エラー
# perl Makefile.PL
Writing Makefile for SNMP
ERROR:
Net-SNMP installed version: 5.3.2.2 => 5.03022
Perl Module Version: 5.0401
These versions must match for perfect support of the module. It is possible
that different versions may work together, but it is strongly recommended
that you make these two versions identical. You can get the Net-SNMP
source code and the associated perl modules directly from
http://www.net-snmp.org/
If you want to continue anyway please set the NETSNMP_DONT_CHECK_VERSION
environmental variable to 1 and re-run the Makefile.PL script.
バージョンがおかしい?
# rpm -qa | grep snmp
net-snmp-libs-5.3.2.2-7.el5_4.2
net-snmp-devel-5.3.2.2-7.el5_4.2
net-snmp-5.3.2.2-7.el5_4.2
⑤こいつがないからっぽい?
yum install net-snmp-perl
ついでに、こいつも
yum -y install net-snmp-utils
# rpm -qa | grep snmp
net-snmp-utils-5.3.2.2-7.el5_4.2
net-snmp-perl-5.3.2.2-7.el5_4.2
net-snmp-libs-5.3.2.2-7.el5_4.2
net-snmp-devel-5.3.2.2-7.el5_4.2
net-snmp-5.3.2.2-7.el5_4.2
参考:http://fedorasrv.com/bbshtml/webpatio/3353.shtml
参考:http://blog.goo.ne.jp/oracool/e/7c5b78dc8ba4ec092c3dbc0f72e8853e
⑥もういっかいサンプル実行。いったっぽい
/usr/src/net-snmp-5.5/perl/SNMP/examples
perl mibwalk.pl localhost public
※、SNMPは個別にCPANからインストールする必要はないっぽい?
⑦snmpd.conf ・・・ エージェント側の設定ファイルの設定
※今回はエージェントも同じホスト。
snmpd.conf ファイルは:snmpd デーモンが参照するファイル
・マネージャの定義
・マネージャに対し参照できるMIBの範囲を定義
・およびMIBによるTrapのエージェント先を定義
ファイルのパスはこちら
/usr/share/snmp/snmpd.conf
⑧マネージャとして動作確認
snmppgetで情報取得 → OK
$ snmpget -v 1 -c localcom 127.0.0.1 .1.3.6.1.2.1.1.5.0
SNMPv2-MIB::sysName.0 = STRING: ホスト名
localcom・・・エージェント側で定義したコミュニティ名
localhost ・・・ エージェント側のホスト名IPアドレス
1.3.6.1.2.1.2.2.1.10.2 ・・・OID
参考:
http://www.atmarkit.co.jp/flinux/rensai/root08/root08b.html
OIDくわしい
http://journal.mycom.co.jp/column/yetanother/081/index.html
Net::SNMPについてモジュール説明
http://fleur.hio.jp/perldoc/mix/lib/Net/SNMP.html
http://fleur.hio.jp/perldoc/mix/lib/Net/SNMP.pod
Net::SNMPTIPS(TRAPだけど)
http://www.arbolbell.jp/perl/2007/12/netsnmp-trap-perlsnmptrap.html
●サンプル実行
エラー
Net::SNMPがひつようらしい
●Net::SNMPをインストール
# perl Makefile.PL
Checking if your kit is complete...
Looks good
Warning: prerequisite Crypt::DES 2.03 not found.
Writing Makefile for Net::SNMP
とりあえずむしで。 ※あとで確認
●サンプル実行 → できた!!
#!/usr/bin/perl
use Net::SNMP;
my ( $session, $error ) = Net::SNMP->session(
-hostname => "localhost",
-community => "localcom",
-port => 161
);
if (!defined($session)) {
printf("ERROR: %s.\n", $error);
exit 1;
}
my $oid = ".1.3.6.1.2.1.1.5.0";
my $result = $session->get_request(
-varbindlist => [$oid]
);
if (!defined($result)) {
printf("ERROR: %s.\n", $session->error);
$session->close;
exit 1;
}
printf("hostname for host '%s' is %s\n",
$session->hostname, $result->{$oid}
);
$session->close;
結果
hostname for host 'localhost' is ホスト名
これでSNMPまわりはのりきれそう。
●SNMPの大きな機能
①情報の要求と応答(get-request)
マネージャからエージェントに監視対象機器の情報を要求します。
エージェントは要求された情報を取得してマネージャに応答します。
②情報の設定要求と応答(set-request)
マネージャからエージェントに監視対象機器の設定変更を要求します。
エージェントは要求された設定変更を実行し、結果をマネージャに応答します。
③状態変化の通知(Trap)
※今回は、get-requestが実装できればいいわなと
★やってみる
①net-snmpのインストール
yum -y install net-snmp
※落とす場合はここから
http://www.net-snmp.org/
net-snmp-5.5.tar.gz
②SNMPDデーモンの起動
③/usr/src/net-snmp-5.5/perl/SNMP/examples
を実行しようとしたら、SNMP.pmがないとのことなので
※参考http://hata.cc/msql/index.htm
②CPANからSNMPを導入
SNMP-5.0401.tar.gz
こちらのMAKEでもmake: net-snmp-config: コマンドが見つかりませんでしたがでるので
③yum install net-snmp-devel
※参考:http://d.hatena.ne.jp/taslam/20080422/1208834598
④もういっかい
エラー
# perl Makefile.PL
Writing Makefile for SNMP
ERROR:
Net-SNMP installed version: 5.3.2.2 => 5.03022
Perl Module Version: 5.0401
These versions must match for perfect support of the module. It is possible
that different versions may work together, but it is strongly recommended
that you make these two versions identical. You can get the Net-SNMP
source code and the associated perl modules directly from
http://www.net-snmp.org/
If you want to continue anyway please set the NETSNMP_DONT_CHECK_VERSION
environmental variable to 1 and re-run the Makefile.PL script.
バージョンがおかしい?
# rpm -qa | grep snmp
net-snmp-libs-5.3.2.2-7.el5_4.2
net-snmp-devel-5.3.2.2-7.el5_4.2
net-snmp-5.3.2.2-7.el5_4.2
⑤こいつがないからっぽい?
yum install net-snmp-perl
ついでに、こいつも
yum -y install net-snmp-utils
# rpm -qa | grep snmp
net-snmp-utils-5.3.2.2-7.el5_4.2
net-snmp-perl-5.3.2.2-7.el5_4.2
net-snmp-libs-5.3.2.2-7.el5_4.2
net-snmp-devel-5.3.2.2-7.el5_4.2
net-snmp-5.3.2.2-7.el5_4.2
参考:http://fedorasrv.com/bbshtml/webpatio/3353.shtml
参考:http://blog.goo.ne.jp/oracool/e/7c5b78dc8ba4ec092c3dbc0f72e8853e
⑥もういっかいサンプル実行。いったっぽい
/usr/src/net-snmp-5.5/perl/SNMP/examples
perl mibwalk.pl localhost public
※、SNMPは個別にCPANからインストールする必要はないっぽい?
⑦snmpd.conf ・・・ エージェント側の設定ファイルの設定
※今回はエージェントも同じホスト。
snmpd.conf ファイルは:snmpd デーモンが参照するファイル
・マネージャの定義
・マネージャに対し参照できるMIBの範囲を定義
・およびMIBによるTrapのエージェント先を定義
ファイルのパスはこちら
/usr/share/snmp/snmpd.conf
⑧マネージャとして動作確認
snmppgetで情報取得 → OK
$ snmpget -v 1 -c localcom 127.0.0.1 .1.3.6.1.2.1.1.5.0
SNMPv2-MIB::sysName.0 = STRING: ホスト名
localcom・・・エージェント側で定義したコミュニティ名
localhost ・・・ エージェント側のホスト名IPアドレス
1.3.6.1.2.1.2.2.1.10.2 ・・・OID
参考:
http://www.atmarkit.co.jp/flinux/rensai/root08/root08b.html
OIDくわしい
http://journal.mycom.co.jp/column/yetanother/081/index.html
Net::SNMPについてモジュール説明
http://fleur.hio.jp/perldoc/mix/lib/Net/SNMP.html
http://fleur.hio.jp/perldoc/mix/lib/Net/SNMP.pod
Net::SNMPTIPS(TRAPだけど)
http://www.arbolbell.jp/perl/2007/12/netsnmp-trap-perlsnmptrap.html
●サンプル実行
エラー
Net::SNMPがひつようらしい
●Net::SNMPをインストール
# perl Makefile.PL
Checking if your kit is complete...
Looks good
Warning: prerequisite Crypt::DES 2.03 not found.
Writing Makefile for Net::SNMP
とりあえずむしで。 ※あとで確認
●サンプル実行 → できた!!
#!/usr/bin/perl
use Net::SNMP;
my ( $session, $error ) = Net::SNMP->session(
-hostname => "localhost",
-community => "localcom",
-port => 161
);
if (!defined($session)) {
printf("ERROR: %s.\n", $error);
exit 1;
}
my $oid = ".1.3.6.1.2.1.1.5.0";
my $result = $session->get_request(
-varbindlist => [$oid]
);
if (!defined($result)) {
printf("ERROR: %s.\n", $session->error);
$session->close;
exit 1;
}
printf("hostname for host '%s' is %s\n",
$session->hostname, $result->{$oid}
);
$session->close;
結果
hostname for host 'localhost' is ホスト名
これでSNMPまわりはのりきれそう。
2010年2月7日日曜日
覚書 PERLでのグラフ描画について
画像描画用のモジュールは、GDモジュールを利用。
①CPANより、ダウンロード
GD-2.44.tar.gz
②perl Makefile.PL でエラー
Notice: Type perl Makefile.PL -h for command-line option summary.
**UNRECOVERABLE ERROR**
Could not find gdlib-config in the search path. Please install libgd 2.0.28 or higher.
If you want to try to compile anyway, please rerun this script with the option --ignore_missing_gd.
※libgdっていうライブラリがないくさい
③いまはいってるのは。。。
# rpm -qa | grep gd-
sysklogd-1.4.1-39.2
gd-2.0.33-9.3.fc6
# rpm -qa | grep libgd -> ない
# rpm -qa | grep libp
libpng-1.2.10-7 ->これはある
libpcap-0.9.4-8.1
→>ないくさい。
④gd-develをインストールすれば、はいってくるっぽい。
yum install gd-devel
※参考:http://www.libgd.org/FAQ#How_do_I_install_gd_on_Fedora_Linux.3F
※http://www.libgd.orgからおとしてもこれる。
⑤あらためて、GBモジュールのコンパイルを試み。
→エラーなし。
⑥サンプル作成したけど、エラー
GD::Graph::は別途ダウンロードが必要みたい
⑦GDGraph-1.44.tar.gzダウンロード
Checking if your kit is complete...
Looks good
Warning: prerequisite GD::Text 0.80 not found.
Writing Makefile for GD::Graph
The automatic tests for GDGraph are not really a solid workout of the
library. The best way to test the package is to run the examples
before installing it. You can run the examples in the samples
directory with `make samples` or by going into that directory, and
just running `make`.
If that fails, please read samples/Makefile.
※無視したけどやっぱだめ。
⑧GDTextUtil-0.86.tar.gzもいれる
→OK
後で見つけた、結局ここの説明がすべてかも。
http://www.gadgety.net/shin/tips/unix/perl-gd.html
日本語化への対応も記載あり。
⑨サンプル スクリプト
#! /usr/bin/perl -wT
use strict;
use CGI;
use GD::Graph::linespoints;
use CGI::Carp qw(fatalsToBrowser);
my @data;
my $querry;
my $graph;
$querry = new CGI;
@data = (["2/1", "2/2", "2/3", "2/4", "2/5", "2/6", "2/8", "2/9", "2/10", "2/11"],
[99,40,35,49,85, 20, 24,45,30,78, 69],
[1,3,5,7,9,10,11,12,13,14,15]);
$graph = new GD::Graph::linespoints();
binmode STDOUT;
$graph->set(
title => "resouce usage rate",
x_label => "Day",
y_label => "Usage Rate % ",
long_ticks => 1,
y_max_value => 100,
y_min_value => 0,
y_tick_number => 10
);
print $querry->header( - type => "image/png", -expires=>"now");
print $graph->plot(\@data)->png;

⑩画像をリンクとするには
<img src="/cgi-bin/gdtest.pl" border="0" /> ※<>半角
これでグラフ描画の課題はひとまず終了かなと。
①CPANより、ダウンロード
GD-2.44.tar.gz
②perl Makefile.PL でエラー
Notice: Type perl Makefile.PL -h for command-line option summary.
**UNRECOVERABLE ERROR**
Could not find gdlib-config in the search path. Please install libgd 2.0.28 or higher.
If you want to try to compile anyway, please rerun this script with the option --ignore_missing_gd.
※libgdっていうライブラリがないくさい
③いまはいってるのは。。。
# rpm -qa | grep gd-
sysklogd-1.4.1-39.2
gd-2.0.33-9.3.fc6
# rpm -qa | grep libgd -> ない
# rpm -qa | grep libp
libpng-1.2.10-7 ->これはある
libpcap-0.9.4-8.1
→>ないくさい。
④gd-develをインストールすれば、はいってくるっぽい。
yum install gd-devel
※参考:http://www.libgd.org/FAQ#How_do_I_install_gd_on_Fedora_Linux.3F
※http://www.libgd.orgからおとしてもこれる。
⑤あらためて、GBモジュールのコンパイルを試み。
→エラーなし。
⑥サンプル作成したけど、エラー
GD::Graph::は別途ダウンロードが必要みたい
⑦GDGraph-1.44.tar.gzダウンロード
Checking if your kit is complete...
Looks good
Warning: prerequisite GD::Text 0.80 not found.
Writing Makefile for GD::Graph
The automatic tests for GDGraph are not really a solid workout of the
library. The best way to test the package is to run the examples
before installing it. You can run the examples in the samples
directory with `make samples` or by going into that directory, and
just running `make`.
If that fails, please read samples/Makefile.
※無視したけどやっぱだめ。
⑧GDTextUtil-0.86.tar.gzもいれる
→OK
後で見つけた、結局ここの説明がすべてかも。
http://www.gadgety.net/shin/tips/unix/perl-gd.html
日本語化への対応も記載あり。
⑨サンプル スクリプト
#! /usr/bin/perl -wT
use strict;
use CGI;
use GD::Graph::linespoints;
use CGI::Carp qw(fatalsToBrowser);
my @data;
my $querry;
my $graph;
$querry = new CGI;
@data = (["2/1", "2/2", "2/3", "2/4", "2/5", "2/6", "2/8", "2/9", "2/10", "2/11"],
[99,40,35,49,85, 20, 24,45,30,78, 69],
[1,3,5,7,9,10,11,12,13,14,15]);
$graph = new GD::Graph::linespoints();
binmode STDOUT;
$graph->set(
title => "resouce usage rate",
x_label => "Day",
y_label => "Usage Rate % ",
long_ticks => 1,
y_max_value => 100,
y_min_value => 0,
y_tick_number => 10
);
print $querry->header( - type => "image/png", -expires=>"now");
print $graph->plot(\@data)->png;

⑩画像をリンクとするには
<img src="/cgi-bin/gdtest.pl" border="0" /> ※<>半角
これでグラフ描画の課題はひとまず終了かなと。
登録:
コメント (Atom)
