Forum Discussion

Ben_Tucker's avatar
12 years ago

Passing arguments to Groovy Script

Can I pass host properties from the portal into a groovy script?

for example, in the \'\'Access remote servers via SSH session\'\' documentation example, the example code starts:

Line 1. import com.santaba.collector.groovyapi.expect.Expect

Line 2. cli = Expect.open(\'\'192.168.211.129\'\', \'\'root\'\', \'\'xxxxxxx\'\')

...

I\'d like to set some host parameters like

myservice.user = user

myservice.pass = 12345

and access them with something like the following:

Line 2. cli = Expect.open( some_hostname_variable , some_properties_map[\'\'myservice.user\'\'], some_properties_map[\'\'myservice.pass\'\'])

does some syntax like that exist? if so, where can I read more about it?

  • Yes - see http://help.logicmonitor.com/using/datasource/creating-datasources/collectors/script-collector/groovy-support/rnSpecifically :rn

    • rn
    • Accessing all host properties - all properties of a host are accessible from groovy scripts via accessing the map hostProps.get(property). With other scripting languages, this requires passing in the properties as parameters. But in groovy, its as simple as accessing the hostname via: hostname = hostProps.get(system.hostname);rn
  • Ben, host properties are split into 2 groups: System properties, and custom properties.rnScreenshot: http://imageshack.us/f/545/sn8u.png/rnSystem properties can be referenced by hostProps.get(system.property) Where property is replaced by the specific property: location, ips, displayname, etc. i.e hostProps.get(system.ips)rn rnCustom properties are usually created with the property.name format. For example: snmp.community, wmi.user, etc; but you can specify the property name any way you like. When referencing the custom property just use the exact name of the property. i.e. hostProps.get(ssh.pass) assuming ssh.pass is specified as a custom property on the host.