Forum Discussion

Lewis_Beard's avatar
2 years ago

Using groovy script to send email (inside a DS or CS)?

We use a resource associated with a specific collector to run scripts using config sources as the mechanism. One of the scripts I am writing now posts to a “UserDisable_Warning” config source a list of users whose suspension is imminent.

I would like to use the config source to also send an email to the user, but I’m worried I’m going to have to do a bunch of crazy stuff to set up things on the collector. But I’d rather not use the collector’s exchange to send email, because I dont have direct access to it (go figure) and some of the datasources I’ve seen look like a serious pain to use (Email_TransitTime, for example) as a template.

I was wondering if anyone knows a simpler way to send email. When a resource has an alert with an escalation chain that contains an email, I was under the assumption that no exchange server or imap or anything else has to be set up on the collector. Or so I thought.

So I’m kinda hoping there is some easy way to avoid having to set up mail stuff on a collector, and just have that collector, via a groovy script, send an email out somehow. Like in my wildest dreams I would import something, and then do java.pipedream.Email.send (“blah@foo.blah”,”LM expiration for blah@foo.blah”,”Dear Blah,\nYour LM account expires in X days.\n\n--MyCompany”).

Or at least something not reliant on setting up email servers or clients or etc on the collector. Maybe I’m completely off in the wrong direction.

What would be the easiest way to have groovy send an email, preferably with me not having a lot of control of my own collector (even though I’m portal admin; work silos, know what I’m sayin’? :) )?

Thanks!

  • I think you’d be better off using the API to enumerate your results and send email from that script. That said, it is possible to use the jakarta module to send email, but you may run into various problems including needing to use OAuth2 (in theory this is supported now, but LM still has not documented it at all).

  • Your best bet is to modify something like Email_RoundTrip or Email_TransitTime to just do the SMTP parts. You will still need to provide some email server to sent to, but it can be M365, gmail or whatever your company uses for email. You don’t need to setup a real email server for this.

    I personally would do these types of tasks outside of LM. Like using a utility server to run scripts on a scheduled basis. But I understand if you don’t have access to something like that.

  • Anonymous's avatar
    Anonymous

    When a resource has an alert with an escalation chain that contains an email, I was under the assumption that no exchange server or imap or anything else has to be set up on the collector. Or so I thought.

    This doesn’t happen in the Collector. It happens from the LM platform in the cloud.

    One very tedious way would be to create configsource instances for each user and have the config check look for the message indicating the expiration is imminent. That would generate a separate alert for each user. You could then route, using alert rules, based on the instance name (this is the tedious part) to each user.

    Instead of routing based on alert rules, you could use external alerting with the multi-instance configsource. That would pass all alerts to a script that runs on the collector. That script would have to ignore all alerts that you don’t want it emailing about. But the rest of the alerts could be processed by the script and send the email. You wouldn’t be tied to groovy at that point, but any script that you could run from the collector bash/zsh shell (i.e. python).

    Alternatively still, there are services out there that let you post data (webhook) and they send the data as an email to the address you specify. You could build a custom HTTP integration to send alerts to that service and have them send the email for you.

    All of these are options, each with advantages and disadvantages. YMMV.

  • Thanks. I guess I will dig in and see what I can find. I dont really have a platform where I can easily do that from, and personally, I like that, as a config source in the portal, I like that if I get hit by a bus, someone else can see that the script exists and is running. Having something running from some unknown place elsewhere creates its own problems, for people who inherit things. :)

    ...

    I do wish that LM had a scripting platform, or a section called “Scripts” that you could just set up scheduled scripts on officially, instead of a CS workaround.

    I can understand that, as a consultant I ran into that all the time where some email is coming from some system no one knows about. In the past I’ve added “fine print” to the notification emails saying that this email was generated on X server or system to help.

    There are other services and packages that are more geared for centrally managing scripts or multiple systems (like RMM). I do see this particular request as kinda falling between monitoring and RMM, and can see someone thinking the monitoring system could be generating it.

  • Anonymous's avatar
    Anonymous

    FWIW: After I get my automation scripts working, I build them into datasources so that they run on the collector as collector tasks. I usually apply the DS to the <portalname>.logicmonitor.com resource and I usually have a primary/backup collector pair or ABCG monitoring that resource so that if something happens, the task just moves over to another collector. The output of the script is just a return code in its most simple. In more complicated ones, I have the script output the final value of various variables in the script (sort of like a watch). I do my script logging through LM Logs, so that the logs are in one place no matter which collector gets the task. 

  • I think you’d be better off using the API to enumerate your results and send email from that script.

    Can you explain what that would involve? I’m already using the API, calling it from within Groovy (the config source) to get my user list, with a filter for active users that have an email set.

    So I have a list of users from the API, but I dont see anything in the API for sending email. So I’m wondering if you could elaborate.

  • Your best bet is to modify something like Email_RoundTrip or Email_TransitTime to just do the SMTP parts. You will still need to provide some email server to sent to, but it can be M365, gmail or whatever your company uses for email. You don’t need to setup a real email server for this.

    I personally would do these types of tasks outside of LM. Like using a utility server to run scripts on a scheduled basis. But I understand if you don’t have access to something like that.

    Thanks. I guess I will dig in and see what I can find. I dont really have a platform where I can easily do that from, and personally, I like that, as a config source in the portal, I like that if I get hit by a bus, someone else can see that the script exists and is running. Having something running from some unknown place elsewhere creates its own problems, for people who inherit things. :)

    But I dont really have easy access to anything. I could call the API from a database I have access to, that can make that call, and get results, and send email from that database, but …. yuck. :)

    I do wish that LM had a scripting platform, or a section called “Scripts” that you could just set up scheduled scripts on officially, instead of a CS workaround.

    Thanks!