Forum Discussion

alan's avatar
3 years ago

Node.js external scripting on new datasource

Hi All,

Has anyone had success with uploading a node.js/javascript script into a new datasource for data collection? I have a very simple script that does an API call and I'm just saving the length of an array I get back in the response. How does an LM collector handle this script with NPM packages?

I'm getting syntax errors after testing the script within the new datasource, the script works find locally.

  • Anonymous's avatar
    Anonymous

    Ok, so you're uploading the script (it's not set to groovy nor powershell). When you say locally, you mean locally on the Collector, right?

    When you execute locally on the collector, what does the command look like?  Probably something like this:

    node app.js arg1 arg2

    If that's the case, you'll need to put "node" in the "Linux/Unix Script" box in the DS, and everything after it "app.js arg1 arg2" into the parameters.

  • Locally on my laptop or another system that has node installed with a few npm packages. I would run it locally like this "node compliance-count.js".

    This is on a new datasource and then selecting upload script file. I tried adding "node" as a parameter.

     

    cleaned up script below

    const axios = require('axios');
    const https = require('https');
    const _ = require('lodash');
    
    const usKEY = 'theapikey'
    
    const us_auth = {
        username: usKEY,
        password: ''
    }
    
    const agent = new https.Agent({
        rejectUnauthorized: false
    });
    
    
    axios.get("https://x.x.x.x/api/v1/system/active-users?number=400", {auth: us_auth, httpsAgent: agent})
    .then(function (response) {
    
        const usData = response.data['active-users']["active-user-records"]["active-user-record"];
        const allFiltered = _.filter(usData, {'endpoint-security-status': 'Not Compliant\nPassed Policies: \nFailed Policies: Crowdstrike\nEliminated Roles: '});
    
        console.log(allFiltered)
        console.log(allFiltered.length)
    })

     

  • I just tried "node compliance-count.js" within the datasource and I get the same syntax error.

  • Anonymous's avatar
    Anonymous

    Ok, the Collector will need Node installed in order for it to be able to run the script. If it's linux, you can simply have node installed and add a shebang line that points to the installation directory. Then you can simply reference the script name in the "Linux/Unix Script" box in the DS. 

    To test that things will work using the Collector, but outside the DS, put your script in /usr/local/logicmonitor/agent/local/bin (give it a different name than the file you uploaded to avoid collisions with the DS). Then run it from that directory. If it works from that directory by calling "node compliance-count.js", then put "node" in the "Linux/Unis Script" box and "compliance-count.js" in the "Parameters" box.

  • That makes sense, I will give that a try and let you know. Thanks!

  • Alright, I had some time to get this setup. Installed node and npm packages on the collector that is monitoring a specific resource. I uploaded the script via the new datasource and it appeared here /usr/local/logicmonitor/agent/local/bin, I am able to run "node compliance-count.js" in that directory and it works.

    Adding the script name "compliance-count.js" to linux/unix script and then parameters "node" is still giving me the same error. I don't think adding "node" to the linux/unix script box will work, it couldnt find the external script.

    /usr/local/logicmonitor/agent/local/bin/compliance-count.js: 1: Syntax error: word unexpected (expecting ")")

  • Anonymous's avatar
    Anonymous
    On 7/8/2022 at 2:01 PM, alan said:

    Adding the script name "compliance-count.js" to linux/unix script and then parameters "node" is still giving me the same error.

    Yep, that's backward. since "node compliance-count.js" works in that directory, I'd try putting a she-bang line at the top of your script pointing to the path of node on that collector. That way you can just put "compliance-count.js" in the linux/unix script and nothing in parameters. The collector will treat the script as executable and pass it to the OS. The OS will read the shebang line and call the script using node.