|
| |
|
Monitoring the disk space using a UNIX script is quite a simple task, whereas doing the same from PL/SQL is more entertaining. This kind of function can be implemented using a Java class and be accessed by PL/SQL. In the class a public function listDf is defined, which executes the operating system command /usr/bin/df -k; the output of the command is stored in a string and returned to the PL/SQL code.
//======================================================================================
public class Cdf {
public Cdf() {
}
public static String listDf() {
String sLs = "";
String s = "";
try
{
sLs = "";
boolean found = false;
Process p = Runtime.getRuntime().exec("/usr/bin/df -k");
InputStream is = p.getInputStream();
DataInputStream dis = new DataInputStream(is);
do{
s = dis.readLine();
if(s != null) {
//System.out.println("ls returns: " + s + "\n");
sLs = sLs + s + "\n";
}
}while (s!= null);
}
catch(IOException io) {
sLs = "There was an error";
}
return sLs;
}
}
DECLARE
sDf VARCHAR2(24000);
sBuf VARCHAR2(2000);
i1 INTEGER := 0;
i2 INTEGER := 0;
ip INTEGER := 0;
pctF INTEGER ;
ipu INTEGER := 0;
sMP VARCHAR2(240);
c sys.utl_smtp.connection;
MIN_SPACE INTEGER := 25;
message VARCHAR2(254);
BEGIN
sDf := sys.listDf;
dbms_output.put_line('the output length is '||TO_CHAR(LENGTH(sDf)));
FOR i in 1..100
LOOP
i1 := INSTR(sDf,'/dev',1,i);
if (i1 <> 0) THEN
--dbms_output.put_line('dev found at position '||TO_CHAR(i1));
if (i2 <> 0) THEN
sBuf := SUBSTR(sDf,i2, i1 -i2);
--dbms_output.put_line(sBuf);
ip := INSTR(sBuf,'%',1,1);
ipu := INSTR(sBuf,'/u',1,1);
IF(ip > 0 AND ipu> 0 ) THEN
dbms_output.put_line(SUBSTR(sBuf, ip + 1, i1 -i2 -ip -1)||
' '||SUBSTR(sBuf, ip - 3, 3));
sMP := SUBSTR(sBuf, ip + 1, i1 -i2 -ip -1);
pctF := 100. - TO_NUMBER(SUBSTR(sBuf, ip - 3, 3));
IF ( pctF < 25 ) THEN
dbms_output.put_line('Chiedo un collegamento ');
c := utl_smtp.open_connection('mail.front-row-seat.com');
utl_smtp.helo(c, 'front-row-seat.com');
utl_smtp.mail(c, 'oracle@mydb.com');
utl_smtp.rcpt(c, 'support@front-row-seat.com');
utl_smtp.open_data(c);
utl_smtp.write_data(c,'Subject' || ':' || sMP ||
' is is low in space: only '||TO_CHAR(pctF)||'% left');
message := sMP ||' is is low in space';
utl_smtp.write_data(c,utl_tcp.CRLF || message);
utl_smtp.close_data(c);
utl_smtp.quit(c);
END IF;
END IF;
END IF;
END IF;
i2 := i1;
END LOOP;
end;
/
Here is the sample output
SQL> set serverout on size 1000000 SQL> @dfFromOracle.sql the output length is 1644 /usr1 69 /usr2 43 /usr3 57 /usr4 3 /usr5 31 /usr6 54 /u01 80 /u03 68 /u05 58 /u02 76 /u08 75 /u09 68 /u04 71 /u06 46 /u10 37 PL/SQL procedure successfully completed.If the space is lower than the threshold MIN_SPACE, an email is sent using the utl_smtp package. |
|
| [Home] [Web Design] [HTML tutorials] [Javascript] [PSP] [About us] [Links] [Anonymous email] [Best hosting] [Daily Oracle Life] [IT jobs in Switzerland] [Web Submission] [Web traffic] | |