ch7.PDF

Similar documents
untitled

untitled

SQL Server SQL Server SQL Mail Windows NT

AL-M200 Series

目錄

untitled

1 o o o CPU o o o o o SQL Server 2005 o CPU o o o o o SQL Server o Microsoft SQL Server 2005

1-1 database columnrow record field 不 DBMS Access Paradox SQL Server Linux MySQL Oracle IBM Informix IBM DB2 Sybase 1-2

SP_ SP_03 JAVA...6 SP_10 SQL...8 SP_ SP_ SP_ SP_ SP_ SP_ SP_ SP_04.NET...33 SP_02 C...37 SP_05

第6章  数据库技术基础

IP505SM_manual_cn.doc

基于UML建模的管理管理信息系统项目案例导航——VB篇

软件概述

一步一步教你搞网站同步镜像!|动易Cms

Oracle 4

幻灯片 1

AL-MX200 Series

User Group SMTP

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

epub 61-2

untitled

ebook140-9

2 2 3 DLight CPU I/O DLight Oracle Solaris (DTrace) C/C++ Solaris DLight DTrace DLight DLight DLight C C++ Fortran CPU I/O DLight AM

IBM Rational ClearQuest Client for Eclipse 1/ IBM Rational ClearQuest Client for Ecl

f2.eps

untitled

Perl

数 据 库 系 统 基 础 2/54 第 6 章 数 据 库 管 理 与 维 护

使用SQL Developer

計畫書封面範例

Chapter 2

一 個 SQL Injection 實 例 的 啟 示 頁 2 / 6 因 此, 在 知 名 網 站 上 看 到 SQL Injection, 讓 人 驚 心, 卻 不 意 外 網 站 專 案 外 包 是 目 前 業 界 的 常 態, 而 在 價 格 取 勝 的 制 度 下, 低 價 得 標 的 S

ebook46-23

Windows RTEMS 1 Danilliu MMI TCP/IP QEMU i386 QEMU ARM POWERPC i386 IPC PC104 uc/os-ii uc/os MMI TCP/IP i386 PORT Linux ecos Linux ecos ecos eco

untitled

目 錄 第 一 章 weberp 簡 介... 6 第 一 節 概 述... 6 第 二 節 安 全 性... 7 第 三 節 功 能... 7 一 銷 售 及 訂 單... 7 二 稅... 8 三 應 收 帳 款... 8 四 存 貨... 8 五 購 買... 9 六 應 付 帳 款... 9

RAID RAID 0 RAID 1 RAID 5 RAID * ( -1)* ( /2)* No Yes Yes Yes A. B. BIOS SATA C. RAID BIOS RAID ( ) D. SATA RAID/AHCI ( ) SATA M.2 SSD ( )

A API Application Programming Interface 见 应 用 程 序 编 程 接 口 ARP Address Resolution Protocol 地 址 解 析 协 议 为 IP 地 址 到 对 应 的 硬 件 地 址 之 间 提 供 动 态 映 射 阿 里 云 内

untitled

目 錄 1. 青 椒 炒 肉 絲 2 2. 茄 汁 燴 魚 片 3 3. 乾 煸 四 季 豆 4 4. 黑 胡 椒 豬 柳 5 5. 香 酥 花 枝 絲 6 6. 薑 絲 魚 片 湯 7 7. 醋 瓦 片 魚 8 8. 燜 燒 辣 味 茄 條 9 9. 炒 三 色 肉 丁 榨 菜 炒

PPBSalesDB.doc

ebook45-5

0SQL SQL SQL SQL SQL 3 SQL DBMS Oracle DBMS DBMS DBMS DBMS RDBMS R DBMS 2 DBMS RDBMS R SQL SQL SQL SQL SELECT au_fname,au_ lname FROM authors ORDER BY

學 科 100% ( 為 單 複 選 題, 每 題 2.5 分, 共 100 分 ) 1. 請 參 閱 附 圖 作 答 : (A) 選 項 A (B) 選 項 B (C) 選 項 C (D) 選 項 D Ans:D 2. 下 列 對 於 資 料 庫 正 規 化 (Normalization) 的 敘

Progperl.PDF

四川省普通高等学校

软件测试(TA07)第一学期考试

Junos Pulse Mobile Security R1 2012, Juniper Networks, Inc.

ebook66-15

R D B M S O R D B M S R D B M S / O R D B M S R D B M S O R D B M S 4 O R D B M S R D B M 3. ORACLE Server O R A C L E U N I X Windows NT w w

PowerPoint Presentation

SA-DK2-U3Rユーザーズマニュアル

Basic System Administration

<4D F736F F D20C9CFBAA3CAD0BCC6CBE3BBFAB5C8BCB6BFBCCAD4C8FDBCB6BFBCCAD4B4F3B8D95FBDA8D2E9B8E55F5F E646F63>

ebook71-13

WebSphere Studio Application Developer IBM Portal Toolkit... 2/21 1. WebSphere Portal Portal WebSphere Application Server stopserver.bat -configfile..

ebook 185-6

目錄 C ontents Chapter MTA Chapter Chapter

els0xu_zh_nf_v8.book Page Wednesday, June, 009 9:5 AM ELS-0/0C.8

Microsoft Word - SupplyIT manual 3_cn_david.doc

ebook140-8

未命名

6-1 Table Column Data Type Row Record 1. DBMS 2. DBMS MySQL Microsoft Access SQL Server Oracle 3. ODBC SQL 1. Structured Query Language 2. IBM

PowerPoint プレゼンテーション

A modified offset min-sum decoding algorithm

Oracle高级复制配置手册_业务广告_.doc

untitled

PowerPoint Presentation

ansoft_setup21.doc

SP5 user guide.PDF

01 SQL Server SQL Server 2008 SQL Server 6-1 SSIS SQL Server ( master ) ( msdb ) SQL Server ( master ) master 6-1 DTS sysadmin 6-1 sysa

untitled

深入理解otter

DB2 (join) SQL DB2 11 SQL DB2 SQL 9.1 DB2 DB2 ( ) SQL ( ) DB2 SQL DB2 DB2 SQL DB2 DB2 SQL DB2 ( DB2 ) DB2 DB2 DB2 SQL DB2 (1) SQL (2) S

PowerPoint Presentation

プリント

习题1

RUN_PC連載_12_.doc

KL DSC DEMO 使用说明

壹 前 言 跟 假 bye-bye-- 齒 再 生 的 奇 蹟 齒 的 樣 貌 雖 然 看 似 簡 單, 但 它 其 實 是 個 複 雜 的 微 小 器 官, 多 年 來, 科 學 家 一 直 在 想 辦 法, 要 製 造 出 一 個 比 假 更 好, 甚 至 跟 天 然 齒 如 出 一 轍 的 齒

untitled

PL600 IPPBX 用户手册_V2.0_.doc

ebook 96-16

Oracle Solaris Studio makefile C C++ Fortran IDE Solaris Linux C/C++/Fortran IDE "Project Properties" IDE makefile 1.

untitled

ebook 165-5

QL1880new2.PDF

VB控件教程大全

Microsoft Word - OPIGIMAC 譯本.doc


IC-900W Wireless Pan & Tilt Wireless Pan & Tilt Remote Control / Night Vision FCC ID:RUJ-LR802UWG

Cadence Poqi

NTSE: Non-Transactional Storage Engine MySQL InnoDB 10 InnoDB +Memcached 5 50% / K C++

ebook 132-6

jdbc:hsqldb:hsql: jdbc:hsqldb:hsqls: jdbc:hsqldb:http: jdbc:hsqldb:https: //localhost // :9500 / /dbserver.somedomain.com /an_alias /enrollme

Microsoft Word htm

温州市政府分散采购

自由軟體教學平台

1 SQL Server 2005 SQL Server Microsoft Windows Server 2003NTFS NTFS SQL Server 2000 Randy Dyess DBA SQL Server SQL Server DBA SQL Server SQL Se

Transcription:

283

2 8 4 7.1 Perl SQL D B D :: O D B C D B D :: S y b a s e

2 8 5

2 8 6 D B D :: O r a c l e

2 8 7 D B D :: S y b a s e D B D :: S y b a s e D B D :: S y b a s e D B D :: P r o x y D B D :: P r o x y D B D :: O D B C u n i x O D B C i O D B C

2 8 8 7.2 DBI use DBI; # $database # $database = "sysadm"; $dbh = DBI->connect("DBI:mysql:$database",$username,$pw); die "Unable to connect: $DBI::errstr\n" unless (defined $dbh); D B D :: m y s q l c o n n n e c t () c o n n e c t () R a i s e E r r o r P r i n t E r r o r $dbh = DBI->connect("DBI:mysql:$database", $username,$pw,{raiseerror => 1); c o n n e c t () d i e

2 8 9 q s o m e t h i n g q {s o m e t h i n g $results=$dbh->do(q{update hosts SET bldg = 'Main' WHERE name = 'bendir'); die "Unable to perform update:$dbi::errstr\n" unless (defined $results); $ r e s u l t s u n d e f S E L E C T p r e p a r e e x e c u t e $sth = $dbh->prepare(q{select * from hosts) or die "Unable to prep our query:".$dbh->errstr."\n"; $rc = $sth->execute or die "Unable to execute our query:".$dbh->errstr."\n"; p r e p a r e () p r e p a r e () e x e c u t e () q \ ' \ "

2 9 0 p r e p a r e () p r e p a r e () p r e p a r e d () d o () p r e p a r e () e x e c u t e () d o e x e c u t e ( ) 0 E 0-1 p r e p a r e () p r e p a r e () e x e c u t e ()? @machines = qw(bendir shimmer sander); $sth = $dbh->prepare(q{select name, ipaddr FROM hosts WHERE name =?); foreach $name (@machines){ $sth->execute($name);...... f o r e a c h W H E R E S E L E C T $sth->prepare( q{select name, ipaddr FROM hosts WHERE (name =? AND bldg =? AND dept =?)); $sth->execute($name,$bldg,$dept);

2 9 1 S E L E C T S E L E C T e x e c u t e () S E L E C T f e t c h r o w _ a r r a y r e f ( ) u n d e f f e t c h r o w _ a r r a y ( ) f e t c h r o w _ h a s h r e f ( ) f e t c h a l l _ a r r a y r e f ( ) u n d e f $sth = $dbh->prepare(q{select name,ipaddr,dept from hosts) or die "Unable to prepare our query: ".$dbh->errstr."\n"; $sth->execute or die "Unable to execute our query: ".$dbh->errstr."\n"; f e t c h r o w _ a r r a y r e f () while ($aref = $sth->fetchrow_arrayref){ print "name: ". $aref->[0]. "\n"; print "ipaddr: ". $aref->[1]. "\n"; print "dept: ". $aref->[2]. "\n";

2 9 2 f e t c h r o w _ h a s h r e f () f e t c h r o w _ a r r a y r e f () f e t c h r o w _ h a s h r e f () while ($href = $sth->fetchrow_hashref){ print "name: ". $href->{name. "\n"; print "ipaddr: ". $href->{ipaddr. "\n"; print "dept: ". $href->{dept. "\n"; f e t c h a l l _ a r r a y r e f () f e t c h r o w _ a r r a y r e f ( ) $aref_aref = $sth->fetchall_arrayref; foreach $rowref (@$aref_aref){ print "name: ". $rowref->[0]. "\n"; print "ipaddr: ". $rowref->[1]. "\n"; print "dept: ". $rowref->[2]. "\n"; print '-'x30,"\n";

2 9 3 $ r o w r e f - > [ 0 ] $ s t h - > { N U M _ O F _ F I E L D S $ s t h - > { N A M E $aref_aref = $sth->fetchall_arrayref; foreach $rowref (@$aref_aref){ for ($i=0; $i < $sth->{num_of_fields;i++;){ print $sth->{name->[$i].": ".$rowref->[$i]."\n"; print '-'x30,"\n"; # # $sth->finish; # $dbh->disconnect; 7.2.1 DBI

2 9 4 b i n d _ c o l () i n d _ c o l u m n s () b i n d _ c o l u m n s () $sth = $dbh->prepare(q{select name,ipaddr,dept from hosts) or die "Unable to prep our query:".$dbh->errstr".\n"; $rc = $sth->execute or die "Unable to execute our query:".$dbh->errstr".\n"; # SELECT $rc = $sth->bind_columns(\$name,\$ipaddr,\$dept); while ($sth->fetchrow_arrayref){ # $name $ipaddr $dept...... 7.3 ODBC use Win32::ODBC;

2 9 5 # MS-SQL Server user DSN # system DSN ODBC_ADD_DSN ODBC_ADD_SYS_DSN if (Win32::ODBC::ConfigDSN( ODBC_ADD_DSN, "SQL Server", ("DSN=PerlSysAdm", "DESCRIPTION=DSN for PerlSysAdm", "SERVER=mssql.happy.edu", # "ADDRESS=192.168.1.4", # IP "DATABASE=sysadm", # "NETWORK=DBMSSOCN", # TCP/IP Socket Lib ))){ print "DSN created\n"; else { die "Unable to create DSN:". Win32::ODBC::Error(). "\n"; W i n 3 2 : : O D B C

2 9 6 # DSN $dbh=new Win32::ODBC("DSN=PerlSysAdm;UID=$username;PWD=$pw;"); die "Unable to connect to DSN PerlSysAdm:". Win32::ODBC::Error(). "\n" unless (defined $dbh); d o () p r e p a r e () e x e c u t e () W i n 32 :: O D B C S q l () W i n 32 :: O D B C W i n 32 :: O D B C n e w $rc = $dbh->sql(q{select * from hosts); S q l () u n d e f d o () u n d e f I N S E R T D E L E T E U P D A T E R o w C o u n t () W i n 32 :: O D B C e x e c u t e () R o w C o u n t () W i n 3 2 : : O D B C p r e p a r e ( ) S q l ( )

2 9 7 d o () if (defined $dbh->sql(q{update hosts SET bldg = 'Main' WHERE name = 'bendir')){ die "Unable to perform update: ".Win32::ODBC::Error()."\n" else { $results = $dbh->rowcount(); S E L E C T W i n 32 :: O D B C F e t c h R o w () u n d e f D a t a () F e t c h R o w () D a t a () D a t a () D a t a H a s h () f e t c h r o w _ h a s h r e f () D a t a H a s h () f e t c h r o w _ h a s h r e f () D a t a H a s h

2 9 8 if ($dbh->fetchrow()){ @ar = $dbh->data();... @ar... if ($dbh->fetchrow()){ $ha = $dbh->datahash('name','ipaddr');... $ha{name $ha{ipaddr... { N A M E W i n 32 :: O D B C F i e l d N a m e s () { N U M _ O F _ F I E L D S F i e l d N a m e s () $dbh->close(); # system DSN ODBC_REMOVE_DSN ODBC_REMOVE_SYS_DSN if (Win32::ODBC::ConfigDSN(ODBC_REMOVE_DSN, "SQL Server","DSN=PerlSysAdm")){ print "DSN deleted\n"; else { die "Unable to delete DSN:".Win32::ODBC::Error()."\n";

2 9 9 7.4 N U L L ---sysadm--- hosts name [char(30)] ipaddr [char(15)] aliases [char(50)] owner [char(40)] dept [char(15)] bldg [char(10)] room [char(4)] manuf [char(10)] model [char(10)] ---hpotter--- customers cid [char(4)] cname [varchar(13)] city [varchar(20)] discnt [real(7)] agents aid [char(3)] aname [varchar(13)]

3 0 0... city [varchar(20)] percent [int(10)] products pid [char(3)] pname [varchar(13)] city [varchar(20)] quantity [int(10)] price [real(7)] orders ordno [int(10)] month [char(3)] cid [char(4)] aid [char(3)] pid [char(3)] qty [int(10)] dollars [real(7)] 7.4.1 DBI MySQL Server use DBI; print "Enter user for connect: "; chomp($user = <STDIN>); print "Enter passwd for $user: "; chomp($pw = <STDIN>); $start= "mysql"; # # $start MySQL $dbh = DBI->connect("DBI:mysql:$start",$user,$pw); die "Unable to connect: ".$DBI::errstr."\n" unless (defined $dbh); # $sth=$dbh->prepare(q{show DATABASES) or die "Unable to prepare show databases: ". $dbh->errstr."\n";

3 0 1 $sth->execute or die "Unable to exec show databases: ". $dbh->errstr."\n"; while ($aref = $sth->fetchrow_arrayref) { push(@dbs,$aref->[0]); $sth->finish; # foreach $db (@dbs) { print "---$db---\n"; $sth=$dbh->prepare(qq{show TABLES FROM $db) or die "Unable to prepare show tables: ". $dbh->errstr."\n"; $sth->execute or die "Unable to exec show tables: ". $dbh->errstr."\n"; @tables=(); while ($aref = $sth->fetchrow_arrayref) { push(@tables,$aref->[0]); $sth->finish; # foreach $table (@tables) { print "\t$table\n"; $sth=$dbh->prepare(qq{show COLUMNS FROM $table FROM $db) or die "Unable to prepare show columns: ". $dbh->errstr."\n"; $sth->execute or die "Unable to exec show columns: ". $dbh->errstr."\n"; while ($aref = $sth->fetchrow_arrayref) { print "\t\t",$aref->[0]," [",$aref->[1],"]\n"; $sth->finish; $dbh->disconnect;

3 0 2 S H O W $ s t a r t c o n n e c t ( ) SHOW TABLES SHOW COLUMNS T e r m :: R e a d k e y 7.4.2 DBI Sybase Server use DBI; print "Enter user for connect: "; chomp($user = <STDIN>); print "Enter passwd for $user: "; chomp($pw = <STDIN>); $dbh = DBI->connect('dbi:Sybase:',$user,$pw); die "Unable to connect: $DBI::errstr\n" unless (defined $dbh); # $sth = $dbh->prepare(q{select name from master.dbo.sysdatabases) or die "Unable to prepare sysdatabases query: ".$dbh->errstr."\n"; $sth->execute or die "Unable to execute sysdatabases query: ".$dbh->errstr."\n";

3 0 3 while ($aref = $sth->fetchrow_arrayref) { push(@dbs, $aref->[0]); $sth->finish; foreach $db (@dbs) { $dbh->do("use $db") or die "Unable to use $db: ".$dbh->errstr."\n"; print "---$db---\n"; # $sth=$dbh->prepare(q{select name FROM sysobjects WHERE type="u") or die "Unable to prepare sysobjects query: ".$dbh->errstr."\n"; $sth->execute or die "Unable to exec sysobjects query: ".$dbh->errstr."\n"; @tables=(); while ($aref = $sth->fetchrow_arrayref) { push(@tables,$aref->[0]); $sth->finish; # " " $dbh->do("use $db") or die "Unable to change to $db: ".$dbh->errstr."\n"; # foreach $table (@tables) { print "\t$table\n"; $sth=$dbh->prepare(qq{exec sp_columns $table) or die "Unable to prepare sp_columns query: ".$dbh->errstr."\n"; $sth->execute or die "Unable to execute sp_columns query: ".$dbh->errstr."\n"; while ($aref = $sth->fetchrow_arrayref) { print "\t\t",$aref->[3]," [",$aref->[5],"(", $aref->[6],")]\n";

3 0 4 $sth->finish; $dbh->disconnect or warn "Unable to disconnect: ".$dbh->errstr."\n"; S E L E C T d a t a b a s e s. o w n e r. t a b l e U S E S E L E C T W H E R E W H E R E WHERE type="u" AND type="s" D B D : : S y b a s e EXEC s p_columns $ s t h = $ d b h- > p r e p a r e (q { S E L E C T n a m e F R O M sy s o b j e c t s W H E R E t y p e = " U " ) " U "

3 0 5 7.4.3 ODBC MS-SQL Server $ d b h - > D r o p C u r s o r ( ) $ s t h - > f i n i s h W i n 3 2 : : O D B C use Win32::ODBC; print "Enter user for connect: "; chomp($user = <STDIN>); print "Enter passwd for $user: "; chomp($pw = <STDIN>); $dsn="sysadm"; # DNS # DSNs $dsn die "Unable to query available DSN's".Win32::ODBC::Error()."\n" unless (%dsnavail = Win32::ODBC::DataSources()); if (!defined $dsnavail{$dsn) { die "unable to create DSN:".Win32::ODBC::Error()."\n" unless (Win32::ODBC::ConfigDSN(ODBC_ADD_DSN, "SQL Server", ("DSN=$dsn", "DESCRIPTION=DSN for PerlSysAdm", "SERVER=mssql.happy.edu", "DATABASE=master", "NETWORK=DBMSSOCN", # TCP/IP Socket Lib )));

3 0 6 # $dbh = new Win32::ODBC("DSN=$dsn;UID=$user;PWD=$pw;"); die "Unable to connect to DSN $dsn:".win32::odbc::error()."\n" unless (defined $dbh); # if (defined $dbh->sql(q{select name from sysdatabases)){ die "Unable to query databases:".win32::odbc::error()."\n"; while ($dbh->fetchrow()){ push(@dbs, $dbh->data("name")); $dbh->dropcursor(); # user-defined foreach $db (@dbs) { if (defined $dbh->sql("use $db")){ die "Unable to change to database $db:". Win32::ODBC::Error(). "\n"; print "---$db---\n"; @tables=(); if (defined $dbh->sql(q{select name from sysobjects WHERE type="u")){ die "Unable to query tables in $db:". Win32::ODBC::Error(). "\n"; while ($dbh->fetchrow()) { push(@tables,$dbh->data("name")); $dbh->dropcursor(); # foreach $table (@tables) { print "\t$table\n"; if (defined $dbh->sql(" {call sp_columns (\'$table\') ")){ die "Unable to query columns in

3 0 7 $table:".win32::odbc::error(). "\n"; while ($dbh->fetchrow()) { @cols=(); @cols=$dbh->data("column_name","type_name","precision"); print "\t\t",$cols[0]," [",$cols[1],"(",$cols[2],")]\n"; $dbh->dropcursor(); $dbh->close(); die "Unable to delete DSN:".Win32::ODBC::Error()."\n" unless (Win32::ODBC::ConfigDSN(ODBC_REMOVE_DSN, "SQL Server","DSN=$dsn")); 7.5 use DBI; # syaccreate < > $admin = 'sa'; print "Enter passwd for $admin: "; chomp($pw = <STDIN>); $user=$argv[0]; # # * * $genpass = reverse join('',reverse split(//,$user)); $genpass.= "-" x (6-length($genpass));

3 0 8 # SQL # SQL # 1 create the database on the USER_DISK device, with the log on USER_LOG # 2 add a login to the server for the user, making the new database the default. # 3 switch to the newly created database # 4 change its owner to be this user @commands = ("create database $user on USER_DISK=5 log on USER_LOG=5", "sp_addlogin $user,\"$genpass\",$user", "use $user", "sp_changedbowner $user"); # $dbh = DBI->connect('dbi:Sybase:',$admin,$pw); die "Unable to connect: $DBI::errstr\n" unless (defined $dbh); # for (@commands) { $dbh->do($_) or die "Unable to $_: ". $dbh->errstr. "\n"; $dbh->disconnect; $ d b h - > d o () use DBI; # syacdelete < > $admin = 'sa'; print "Enter passwd for $admin: "; chomp($pw = <STDIN>); $user=$argv[0]; # SQL

3 0 9 # # drop the user's server login @commands = ("drop database $user", "sp_droplogin $user"); # $dbh = DBI->connect('dbi:Sybase:',$admin,$pw); die "Unable to connect: $DBI::errstr\n" unless(defined $dbh); # for (@commands) { $dbh->do($_) or die "Unable to $_: ". $dbh->errstr. "\n"; $dbh->disconnect or warn "Unable to disconnect: ". $dbh->errstr. "\n"; 7.6

3 1 0 7.6.1 d l ddddddd 15.23%/5MB hpotter-------- 0.90%/5MB ddddddd 15.23%/5MB dumbledore----- 1.52%/5MB dddddddd 16.48%/5MB hgranger------- 1.52%/5MB ddddddd 15.23%/5MB rweasley------- l 3.40%/5MB ddddddddddddddddddddddddddd 54.39%/2MB hagrid--------- - no log use DBI; $admin = 'sa'; print "Enter passwd for $admin: ";

3 1 1 chomp($pw = <STDIN>); $pages = 2; # 2k # $dbh = DBI->connect('dbi:Sybase:',$admin,$pw); die "Unable to connect: $DBI::errstr\n" unless (defined $dbh); # $sth = $dbh->prepare(q{select name from sysdatabases) or die "Unable to prepare sysdatabases query: ".$dbh->errstr."\n"; $sth->execute or die "Unable to execute sysdatabases query: ".$dbh->errstr."\n"; while ($aref = $sth->fetchrow_arrayref) { push(@dbs, $aref->[0]); $sth->finish; # foreach $db (@dbs) { # size $size = &querysum(qq{select size FROM master.dbo.sysusages WHERE dbid = db_id(\'$db\') AND segmap!= 4); # size $logsize = &querysum(qq{select size FROM master.dbo.sysusages WHERE dbid = db_id(\'$db\') AND segmap = 4); # $dbh->do(q{use $db) or die "Unable to change to $db: ".$dbh->errstr."\n"; # reserved_pgs # " " doampg " " ioampg

3 1 2 # 2KB $used=&querysum(q{select reserved_pgs(id,doampg)+reserved_pgs(id,ioampg) FROM sysindexes WHERE id!= 8); # $logused=&querysum(q{select reserved_pgs(id, doampg) FROM sysindexes WHERE id=8); # &graph($db,$size,$logsize,$used,$logused); $dbh->disconnect; # / SELECT sub querysum { my($query) = shift; my($sth,$aref,$sum); $sth = $dbh->prepare($query) or die "Unable to prepare $query: ".$dbh->errstr."\n"; $sth->execute or die "Unable to exec $query: ".$dbh->errstr."\n"; while ($aref=$sth->fetchrow_arrayref) { $sum += $aref->[0]; $sth->finish; $sum; # sub graph { my($dbname,$size,$logsize,$used,$logused) = @_;

3 1 3 # print ' 'x15. ' '.'d'x (50 *($used/$size)). ' 'x (50-(50*($used/$size))). ' '; # MB printf("%.2f",($used/$size*100)); print "%/". (($size * $pages)/1024)."mb\n"; print $dbname.'-'x(14-length($dbname)).'- '.(' 'x 49)." \n"; if (defined $logsize) { # print ' 'x15. ' '. 'l'x (50 *($logused/$logsize)). ' 'x (50-(50*($logused/$logsize))). ' '; # MB printf("%.2f",($logused/$logsize*100)); print "%/". (($logsize * $pages)/1024)."mb\n"; else { # print ' 'x15. " - no log".(' 'x 41)." \n"; print "\n"; q u e r y s u m () S U M S U M q u e r y s u m () S U M ()

3 1 4 D I S T I N C T 7.6.2 SQL Server CPU use DBI; $syadmin = "sa"; print "Sybase admin passwd: "; chomp($sypw = <STDIN>); $msadmin = "sa"; print "MS-SQL admin passwd: "; chomp($mspw = <STDIN>); # Sybase Server

3 1 5 $sydbh = DBI->connect("dbi:Sybase:server=SYBASE",$syadmin,$sypw); die "Unable to connect to sybase server: $DBI::errstr\n" unless (defined $sydbh); # ChopBlanks $sydbh->{chopblanks = 1; # MS-SQL Server DBD::Sybase $msdbh = DBI->connect("dbi:Sybase:server=MSSQL",$msadmin,$mspw); die "Unable to connect to mssql server: $DBI::errstr\n" unless (defined $msdbh); # ChopBlanks $msdbh->{chopblanks = 1; $ =1; # STDOUT I/O # signal handler " " $SIG{INT = sub {$byebye = 1;; # while (1) { last if ($byebye); # sp_monitor $systh = $sydbh->prepare(q{sp_monitor) or die "Unable to prepare sy sp_monitor:".$sydbh->errstr."\n"; $systh->execute or die "Unable to execute sy sp_monitor:".$sydbh->errstr."\n"; # # cpu_busy while($href = $systh->fetchrow_hashref or $systh->{syb_more_results) { # last if (defined $href->{cpu_busy); $systh->finish; # % for (keys %{$href) {

3 1 6 $href->{$_ =~ s/.*-(\d+%)/\1/; # $info = "Sybase: (".$href->{cpu_busy." CPU), ". "(".$href->{io_busy." IO), ". "(".$href->{idle." idle) "; # MS-SQL Server $mssth = $msdbh->prepare(q{sp_monitor) or die "Unable to prepare ms sp_monitor:".$msdbh->errstr."\n"; $mssth->execute or die "Unable to execute ms sp_monitor:".$msdbh->errstr."\n"; while($href = $mssth->fetchrow_hashref or $mssth->{syb_more_results) { # last if (defined $href->{cpu_busy); $mssth->finish; # % for (keys %{$href) { $href->{$_ =~ s/.*-(\d+%)/\1/; $info.= "MSSQL: (". $href->{'cpu_busy'." CPU), ". "(".$href->{'io_busy'." IO), ". "(".$href->{'idle'." idle)"; print " "x78,"\r"; print $info,"\r"; sleep(5) unless ($byebye); # $sydbh->disconnect; $msdbh->disconnect;

3 1 7 Sybase: (33% CPU), (33% IO), (0% idle) MSSQL: (0% CPU), (0% IO), (100% idle) s p _ m o n i t o r s p _ m o n i t o r last_run current_run seconds --------------------------- --------------------------- ----------- Aug 3 1998 12:05AM Aug 3 1998 12:05AM 1 cpu_busy io_busy idle ------------------------- ------------------------- ------------------------- 0(0)-0% 0(0)-0% 40335(0)-0% packets_received packets_sent packet_errors ------------------------- ------------------------- ------------------------- 1648(0) 1635(0) 0(0) total_read total_write total_errors connections ------------------- ------------------- ------------------- ------------------ 391(0) 180(0) 0(0) 11(0) s p _ m o n i t o r s p _ m o n i t o r D B D :: S y b a s e while($href = $systh->fetchrow_hashref or $systh->{syb_more_results) { last if (defined $href->{cpu_busy); I N T

3 1 8 s p _ m o n i t o r 7.7 D B I D B D :: m y s q l D B D :: S y b a s e W i n 32 :: O D B C 7.8 7.8.1 SQL 7.8.2 DBI

3 1 9 7.8.3 ODBC W i n 32 :: O D B C W i n 32 :: O D B C 7.8.4 D B D :: S y b a s e

3 2 0