ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Unsolved I need this script to email the log it generates

    IT Discussion
    scripting email log freepbx
    4
    24
    2.7k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • JaredBuschJ
      JaredBusch
      last edited by

      I'm tired of dealing with shit for a while. So dumping it here. I'll do my own searching later if nothing gets posted.

      I just finished updating this script and the next step will be to have it email the results.
      https://github.com/sorvani/freepbx-helper-scripts/blob/master/InitialSetup/update.sh

      It is designed only for FreePBX systems, so you can assume the PBX already has valid email methods setup. By default, postfix should be working, because that is the FreePBX design.

      1 Reply Last reply Reply Quote 0
      • JaredBuschJ
        JaredBusch
        last edited by JaredBusch

        Likely going to add an if check later also, if there was a kernel update during the yum upgrade to output that as a need to reboot.

        aa3ce112-5464-4a91-b205-ee345cacb19f-image.png

        1 Reply Last reply Reply Quote 0
        • travisdh1T
          travisdh1
          last edited by

          Found here: https://superuser.com/questions/1020680/how-to-mail-output-of-shell-script-as-a-cron-job because my memory is so rusty.

          Have to redirect the script output from stdout, so something like
          sudo ./update.sh 2>&1 | tee output.txt | mail -s "Subject FreePBX update" [email protected]

          JaredBuschJ 1 Reply Last reply Reply Quote 0
          • JaredBuschJ
            JaredBusch @travisdh1
            last edited by JaredBusch

            @travisdh1 I need the local output still, that why it’s creating a text log file. I want that log file emailed to me, not simply the output of the script piped to the mail command. I did assume that I would use the mail command, like I said I have not had the time to look it up yet (the syntax).

            Edited to de-Sirify the post.

            1 Reply Last reply Reply Quote 1
            • 1
              1337
              last edited by

              @JaredBusch I'm not sure exactly what you're looking for except to email the textfile?

              Regardless I suggest putting some more information in the textfile such as hostname or otherthings that would identify what system it is.
              Also perhaps timestamp each output then you'll know how long something takes and you'll know what it was executed on the system.

              JaredBuschJ 3 Replies Last reply Reply Quote 0
              • JaredBuschJ
                JaredBusch @1337
                last edited by

                @Pete-S said in I need this script to email the log it generates:

                Regardless I suggest putting some more information in the textfile such as hostname or otherthings that would identify what system it is.

                None of that needs to be in the log file. The subject line of the email, yes.

                1 Reply Last reply Reply Quote 0
                • JaredBuschJ
                  JaredBusch @1337
                  last edited by

                  @Pete-S said in I need this script to email the log it generates:

                  Also perhaps timestamp each output then you'll know how long something takes and you'll know what it was executed on the system.

                  Not something that is needed to be known.

                  1 Reply Last reply Reply Quote 0
                  • JaredBuschJ
                    JaredBusch @1337
                    last edited by

                    @Pete-S said in I need this script to email the log it generates:

                    @JaredBusch I'm not sure exactly what you're looking for except to email the textfile?

                    That is correct.

                    1 Reply Last reply Reply Quote 0
                    • JaredBuschJ
                      JaredBusch
                      last edited by

                      So, no time to deal with updating and testing the script, today.

                      But since such a simple post, posted to hopefully generate a conversation, did not do anything helpful....

                      The basic mail syntax to send an email and attachment is

                      echo "Some Message Body Text" | mail -s "Some Subject Line" [email protected] -A file.txt
                      
                      1 1 Reply Last reply Reply Quote 1
                      • 1
                        1337 @JaredBusch
                        last edited by

                        @JaredBusch said in I need this script to email the log it generates:

                        So, no time to deal with updating and testing the script, today.

                        But since such a simple post, posted to hopefully generate a conversation, did not do anything helpful....

                        The basic mail syntax to send an email and attachment is

                        echo "Some Message Body Text" | mail -s "Some Subject Line" [email protected] -A file.txt
                        

                        I found it interesting because it's what I was discussion in the other thread about notification emails.

                        But I haven't researched it enough yet.
                        For instance what is the difference between mail and mailx? What happens if the mail bounces? Is the mail cached for delivery if it can't be delivered?

                        JaredBuschJ 1 Reply Last reply Reply Quote 0
                        • JaredBuschJ
                          JaredBusch @1337
                          last edited by

                          @Pete-S said in I need this script to email the log it generates:

                          I found it interesting because it's what I was discussion in the other thread about notification emails.

                          I'm not after alerting. That is not the purpose of this email. It is for a historical record only.

                          The way I handle this type of stuff is that it goes to a folder and is marked read by a rule.

                          Then about once a year I purge anything older than a year.

                          1 1 Reply Last reply Reply Quote 0
                          • 1
                            1337 @JaredBusch
                            last edited by 1337

                            @JaredBusch said in I need this script to email the log it generates:

                            @Pete-S said in I need this script to email the log it generates:

                            I found it interesting because it's what I was discussion in the other thread about notification emails.

                            I'm not after alerting. That is not the purpose of this email. It is for a historical record only.

                            The way I handle this type of stuff is that it goes to a folder and is marked read by a rule.

                            Then about once a year I purge anything older than a year.

                            Yeah, I understood - you just wanted to have the logs mailed to you.

                            I'd call that a notification (not an alert). The script notifies you that it has been executed and what the results where. You don't intend to look at it, unless you have to, but it's still a notification nevertheless.

                            1 Reply Last reply Reply Quote 0
                            • stacksofplatesS
                              stacksofplates
                              last edited by stacksofplates

                              you could use mailgun. I just wrote this tiny app that will send the contents of a file.

                              package main
                              
                              import (
                              	"context"
                              	"fmt"
                              	"github.com/mailgun/mailgun-go/v3"
                              	"io/ioutil"
                              	"os"
                              	"time"
                              )
                              
                              func main() {
                              	mg := mailgun.NewMailgun(os.Getenv("DOMAIN"), os.Getenv("API_KEY"))
                              	data, err := ioutil.ReadFile(os.Args[1])
                              	if err != nil {
                              		fmt.Println(err)
                              	}
                              	m := mg.NewMessage(
                              		"[email protected]",
                              		"Test Log",
                              		string(data),
                              		os.Getenv("SEND_ADDRESS"),
                                              )
                              
                              	ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
                              	defer cancel()
                              
                              	_, id, err := mg.Send(ctx, m)
                              	if err != nil {
                              		fmt.Println(err)
                              	}
                              	fmt.Println(id)
                              }
                              

                              I lazily take the first argument as the file with the contents you want to send. Just have env vars for the recipient address, your api key and domain (or hard code them). You could take flags and whatever, but this was free and I'm lazy.

                              1 1 Reply Last reply Reply Quote 0
                              • 1
                                1337 @stacksofplates
                                last edited by 1337

                                @stacksofplates said in I need this script to email the log it generates:

                                you could use mailgun. I just wrote this tiny app that will send the contents of a file.

                                I lazily take the first argument as the file with the contents you want to send. Just have env vars for the recipient address, your api key and domain (or hard code them). You could take flags and whatever, but this was free and I'm lazy.

                                There is one problem with the api approach I think.
                                I believe tht if you use postfix to deliver the message over SMTP it will put the message in postfix' queue. So if for some reason the mail can't be delivered at that particular time, for instance because the firewall is being rebooted or there is a problem with the mailservice, it will try again later. Using the API you will just a timeout error and that was it.

                                I'm not a mailgun user but I assume they have a SMTP relay as well?

                                In any case since we like zoho, I intend to try the new Zoho TransMail service. They only send transactional mail, not marketing, and I think it was $2.5 or something like that per 10K emails. They have both api and SMTP.

                                stacksofplatesS 1 Reply Last reply Reply Quote 0
                                • stacksofplatesS
                                  stacksofplates @1337
                                  last edited by

                                  @Pete-S said in I need this script to email the log it generates:

                                  @stacksofplates said in I need this script to email the log it generates:

                                  you could use mailgun. I just wrote this tiny app that will send the contents of a file.

                                  I lazily take the first argument as the file with the contents you want to send. Just have env vars for the recipient address, your api key and domain (or hard code them). You could take flags and whatever, but this was free and I'm lazy.

                                  There is one problem with the api approach I think.
                                  I believe tht if you use postfix to deliver the message over SMTP it will put the message in postfix' queue. So if for some reason the mail can't be delivered at that particular time, for instance because the firewall is being rebooted or there is a problem with the mailservice, it will try again later. Using the API you will just a timeout error and that was it.

                                  I'm not a mailgun user but I assume they have a SMTP relay as well?

                                  In any case since we like zoho, I intend to try the new Zoho TransMail service. They only send transactional mail, not marketing, and I think it was $2.5 or something like that per 10K emails. They have both api and SMTP.

                                  That's no different than postfix. It would be pretty trivial to add a retry block for a timeout condition. Postfix has it built in, you'd need the 5 lines of code here or whatever would be needed. And this will work on literally any system that the binary can be compiled for:

                                  • aix
                                  • android
                                  • darwin
                                  • dragonfly
                                  • freebsd
                                  • hurd
                                  • illumos
                                  • js
                                  • linux
                                  • nacl
                                  • netbsd
                                  • openbsd
                                  • plan9
                                  • solaris
                                  • windows
                                  • zos
                                  1 Reply Last reply Reply Quote 1
                                  • stacksofplatesS
                                    stacksofplates
                                    last edited by

                                    Another plus for an API is that snmp is commonly blocked from providers and only enabled with some kind of request.

                                    Also if you want to integrate any other notification (text, telegram notification, slack, etc) it would be trivial to add with this approach.

                                    Not saying API is the best way, but it definitely has advantages.

                                    JaredBuschJ 1 Reply Last reply Reply Quote 0
                                    • JaredBuschJ
                                      JaredBusch @stacksofplates
                                      last edited by

                                      @stacksofplates said in I need this script to email the log it generates:

                                      Another plus for an API is that snmp is commonly blocked from providers and only enabled with some kind of request.

                                      Also if you want to integrate any other notification (text, telegram notification, slack, etc) it would be trivial to add with this approach.

                                      Not saying API is the best way, but it definitely has advantages.

                                      I have not had time to look at what you posted yet but I plan on it thank you

                                      stacksofplatesS 1 Reply Last reply Reply Quote 1
                                      • stacksofplatesS
                                        stacksofplates @JaredBusch
                                        last edited by

                                        @JaredBusch said in I need this script to email the log it generates:

                                        @stacksofplates said in I need this script to email the log it generates:

                                        Another plus for an API is that snmp is commonly blocked from providers and only enabled with some kind of request.

                                        Also if you want to integrate any other notification (text, telegram notification, slack, etc) it would be trivial to add with this approach.

                                        Not saying API is the best way, but it definitely has advantages.

                                        I have not had time to look at what you posted yet but I plan on it thank you

                                        Yeah no problem. It's really simple so it could be optimized quite a bit with maybe like 30 mins of work.

                                        1 1 Reply Last reply Reply Quote 0
                                        • 1
                                          1337 @stacksofplates
                                          last edited by

                                          @stacksofplates said in I need this script to email the log it generates:

                                          @JaredBusch said in I need this script to email the log it generates:

                                          @stacksofplates said in I need this script to email the log it generates:

                                          Another plus for an API is that snmp is commonly blocked from providers and only enabled with some kind of request.

                                          Also if you want to integrate any other notification (text, telegram notification, slack, etc) it would be trivial to add with this approach.

                                          Not saying API is the best way, but it definitely has advantages.

                                          I have not had time to look at what you posted yet but I plan on it thank you

                                          Yeah no problem. It's really simple so it could be optimized quite a bit with maybe like 30 mins of work.

                                          Wouldn't just using curl do the job equally well of sending the message to mailgun?

                                          stacksofplatesS 1 Reply Last reply Reply Quote 0
                                          • stacksofplatesS
                                            stacksofplates @1337
                                            last edited by

                                            @Pete-S said in I need this script to email the log it generates:

                                            @stacksofplates said in I need this script to email the log it generates:

                                            @JaredBusch said in I need this script to email the log it generates:

                                            @stacksofplates said in I need this script to email the log it generates:

                                            Another plus for an API is that snmp is commonly blocked from providers and only enabled with some kind of request.

                                            Also if you want to integrate any other notification (text, telegram notification, slack, etc) it would be trivial to add with this approach.

                                            Not saying API is the best way, but it definitely has advantages.

                                            I have not had time to look at what you posted yet but I plan on it thank you

                                            Yeah no problem. It's really simple so it could be optimized quite a bit with maybe like 30 mins of work.

                                            Wouldn't just using curl do the job equally well of sending the message to mailgun?

                                            Yep. If you really like handling errors and responses in shell scripting it's fine. Unless you use it on Windows where cURL is not really cURL.

                                            I'm not doing raw API calls with the example above. That's using their Go package which simplifies a lot of the API information.

                                            1 1 Reply Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • First post
                                              Last post