Subscribing to SAM notifications with Smarts perl API

I've been asked whether the Smarts perl API can be used to subscribe to SAM notifications. The answer is yes - but there isnt a native library for it, so you have to do the hard work yourself.

Here's an example script to demonstrate one way to do this. Just add your own logic to the processNotificationChange and/or processNotificationRemoval routines.

use InCharge::session;

my $nlName = "Default";

my $s = InCharge::session->init();

my $nlName = $ARGV[0];
die "Usage: sm_perl nlsubscribe.pl -s DOMAIN NLNAME\n" unless (defined $nlName);

my $manager = $s->object("ICS-NLManager");
my $nlObjName = $manager->getListObjectName($nlName);
my $nlObj = $s->object("ICS_NotificationList", $nlObjName);
die "NL '$nlName' not found\n" if ($nlObj->isNull());
my @colInfo = $nlObj->get("ColumnInfo");

my $o = $s->observer();
$s->propertySubscribe( $nlObj, "NotificationDataChange", 30 );
$s->propertySubscribe( $nlObj, "NotificationDataRemoval", 30 );

while ( @event = $o->receiveEvent() ) {
	if ($event[1] eq "ATTR_CHANGE") {
		my @fieldValues = @{$event[5]};
		my %fields = ( );
		foreach ( @colInfo ) { $fields{$_->[1]} = $fieldValues[$_->[0]]; }
		next if ($fields{EventState} eq "UNINITIALIZED");
		if ($event[4] eq "NotificationDataChange") {
			processNotificationChange( \%fields );
		} elsif ($event[4] eq "NotificationDataRemoval") {
			processNotificationRemoval( \%fields );
		}
	}
}

sub processNotificationChange {
	my ( $n ) = @_;
	print "CHANGE: $n->{EventState}: $n->{ClassDisplayName} :: $n->{InstanceDisplayName}"
		." :: $n->{EventDisplayName} ($n->{Certainty})\n";
}

sub processNotificationRemoval {
	my ( $n ) = @_;
	print "REMOVE: $n->{EventState}: $n->{ClassDisplayName} :: $n->{InstanceDisplayName}"
		." :: $n->{EventDisplayName} ($n->{Certainty})\n";
}
Scroll to Top