Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing critical alarm details to txt file / SQL

Status
Not open for further replies.

SteveTr

ISP
Feb 11, 2004
2
GB
Hi all,

When a critical alarm is detected in NNM I need the data to be written to an SQL DB. Failing this anyone know how to simply write the info to a txt file?

Using NNM v 7.01

Thanks in advance
 
easiest way, is with perl scripts. if you know at least a little about perl, and install it ( you're almost 90% of the way there

in the event configuration, just enter "perl write_error.pl \"this is my error message\""

(ps. make sure perl is a trusted command, ie. go to $openview/conf/trustedcmds.conf/ and create a file called NNM and enter this one line : "perl=$PERL_BIN\\perl.exe")

and the perl script would be something like. . .

"
$strTextToWriteToFile = (shift)

open (OUTPUT, ">>output.txt");

print OUTPUT "$strTextToWriteToFile\n";

close (OUTPUT);
"

or something like that. . .

and, realisitcally, exporting to a database wouldn't be a whole lot more difficult, but that depends on your experience with SQL and such.

if you want some help with the SQL version or this one, just let me know, and i'll see what i can do. i've got a perl script that creates Remedy trouble tickets by directly writing to the DB, so i might have what you're looking for. . .

- alex
 
Hi Rovent,

You are a star.....

This appears to be the way to do it :)... OK, so i'm now getting it written to a text file whenever the event alarm fires, which is excellent.

Would you (or anyone else) be willing to share the Perl script you have for directly writing to your DB? From there i will *hopefully* be able to amend to fit our setup...

Many thanks in advance

Steve
 
Steve -

I suppose this belongs in the "Perl" forum, but maybe no one will notice :). Here's the script, it's pretty easy, and actually a lot like the "insert to text file" version.

you would call this the same way: "perl insert_to_db.pl value1 [value2 value3 ...]"

i believe DBI comes with ActivePerl, so with a default install, you should be good to go. This script works well just as a perl script, but takes a little bit of tweaking to get it to work as a CGI script off of a web server, especially if it's an Access Database off of an IIS server.

the worst part is getting the DBI connect to work. if you haven't played with this before, go into the "Database Sources" icon in the control panel (if you're using Windows and not Linux/Unix.) and add a 'User DSN' (Data Source Name).

like i said, give it a shot, and definitely write back if it's not working, or you have any questions, i'll be glad to help.

- alex

################# BEGIN INSERT_TO_DB.PL ##############

#!D:\PERL\bin\perl.exe

use DBI;

################### GET PASSED INFO #################

# add a "shift" to the right hand side for each value being read in from the command line

($Value1 [, $Value2, $Value3, [...]) = (shift [, shift, shift, [...]);

######### CONNECT TO DATABASE ######################

## DBI->connect('dbi:[DATABASE_DRIVER]:[DATA_SOURCE_NAME]','USER_NAME','PASS_WORD');

$strUserName = "MyUserName";
$strPassword = "MyPassword";

$hndDBHandle = DBI->connect('dbi:ODBC:MyDatabase','$strUserName','$strPassword') || print "Cannot connect to the DB: $!<BR>";

##### CONFIGURE HANDLE TO BE ABLE TO READ REALLY LONG VARIABLES ###################

$hndDBHandle->{'LongReadLen'} = 6000 ; # to better handle long variable strings
$hndDBHandle->{LongTruncOk} = 1 ; # TRUE. if it's longer than 6000 characters, just truncate the dumb thing

##### BUILD SQL QUERY ############################

### format: "insert into TABLE_NAME (FIELD1 [, FIELD2, FIELD3, ...]) VALUES (? [, ?, ?, ...])

$strSQLQuery = "INSERT INTO tblMaster (field) VALUES (?)";

##### ASSIGN SQL QUERY TO A HANDLE ######################

$hndStringHandle = $hndDBHandle->prepare($strSQLQuery) || print "Cannot prepare query: $!<BR>";

##### EXECUTE SQL QUERY VIA HANDLE ###################

### why are we doing it this way? so people can't pass crazy parameters that may crash Perl, SQL, Access, or whatever else is involved.

@arrValues = ("$Value1");

$hndStringHandle->execute(@arrValues) || print "Could not execute query: $1<P>";

##### DESTRUCT STRING HANDLE #############################

$hndStringHandle->finish || print "Cannot finish: $!<BR>";

##### DESTRUCT DATABASE HANDLE ##########################

$hndDBHandle->disconnect() || print "Cannot disconnect from DB: $!<BR>";

##### EXIT ##############################################

print "success!\n";

exit 0;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top