DX Unified Infrastructure Management

  • 1.  Create Formatted table in HTML e-mail from Lua for Disk Usage

    Posted Mar 11, 2014 06:56 PM

    Hello,

      I am currently working on a script to generate an e-mail based on active CDM alarms.  The alarms are for Disk Uitlization within my system.  I am currently creating a csv file of disks over 80 % with the attached script.  However, the file when opened has not formatting, and I want to have this data dumped to an e-mail with HTML formatting.  This is not unlike the example that came with the NAS robot "example-html-email-alarms"  However, I want to provide my own data as I show in my script.  Can someone please assist with an example or help me out to configure the HTM script.  I am not a programmer but have been digging deep into scripting in Lua, and actually enjoying it.  Let me know if you need more information. 

     

    Im attaching an example of my csv file output and a screenshot of what I would like the e-mail to look like.

     

    al = alarm.list("prid","%cdm%")

     

    --Disk use Reports

          recipient    = "dhayes"

     

    --Disk over 80% Report

          fname = "\\\\server\\root\\CSG\\Nimsoft\\disksover80.csv"

    file.create (fname)

    file.write (fname,"Server,Disk,Free Space,Threshold,Size")

         

    for i,a inpairs(al) do

                      s,e =string.find(a.message,"disk free on ")

    if s ~= nil then

         

                   process1 = string.sub(a.message,s)

                        

    -- Get Current Disk Name

                            proctemp1 = string.sub(a.message,e+1)

                             s1,e1 = string.find(proctemp1," is now")

                            process1 = (string.sub(proctemp1,1,s1-1))

                     

    -- Get Current Disk Value          

                            s,e =string.find(a.message,"is now ")

                            proctemp2 =string.sub(a.message,e+1)

                            s1,e1 =string.find(proctemp2,"%%, which is below")

                            process2 = (string.sub(proctemp2,1,s1-1))

                     

    -- Get configured Configured Threshold

                            s2,e2 =string.find(proctemp1,"threshold %(")

                            proctemp3 =string.sub(proctemp1,e2+1)

                            s3,e3 =string.find(proctemp3,"%%%)")

                            process3 = (string.sub(proctemp3,1,s3-1))

                            s,e =string.find(a.message,"total size ")

                            proctemp4 =string.sub(a.message,e+1)

                 

    file.write (fname, sprintf ("\n%s,%s,%s%%,%s%%,%s",a.hostname,process1,process2,process3,proctemp4))

                  

    else

                  

    end

    Attachment(s)

    csv
    disksover80.csv   133 B 1 version


  • 2.  Re: Create Formatted table in HTML e-mail from Lua for Disk Usage

    Posted Mar 11, 2014 08:06 PM

    Hi,

     

    I'm not sure what the part is that you're struggling with. Your CSV seems correctly formatted, but I'm not sure why you'd need it for creating a HTML file. Is it the HTML structure that is the problem?

     

    -jon



  • 3.  Re: Create Formatted table in HTML e-mail from Lua for Disk Usage
    Best Answer

    Posted Mar 12, 2014 11:54 PM

    Here is some code that you can add to your script that might help...

     

    --------------------------------------------------------------------------------
    -- BEFORE the for loop
    --------------------------------------------------------------------------------
    
    ROW_FORMAT =  -- multi-line quote...
    [[
      <TR>
        <TD>%s</TD>
        <TD>%s</TD>
        <TD>%s%%</TD>
        <TD>%s%%</TD>
        <TD>%s</TD>
      </TR>
    ]]
    
    html = -- multi-line quote...
    [[
    <HTML>
    <HEAD>
    <TITLE>Disk Report</TITLE>
    </HEAD>
    <BODY>
    <H2>'Disk Utilization > 90% on All Devices' threshold</H2>
    <TABLE BORDER="1" CELLSPACING="0" CELLPADDING="5">
      <TR>
        <TH>Server</TH>
        <TH>Disk</TH>
        <TH>Free Space</TH>
        <TH>Threshold</TH>
        <TH>Disk Size</TH>
      </TR>
    ]]
    
    --------------------------------------------------------------------------------
    -- INSIDE the for loop and if statement - at the bottom
    -------------------------------------------------------------------------------- html = html..sprintf(ROW_FORMAT, a.hostname, process1, process2, process3, proctemp4) -------------------------------------------------------------------------------- -- AFTER the for loop -------------------------------------------------------------------------------- html = html.. -- multi-line quote... [[ </TABLE> </BODY> </HTML> ]]

    The formatting may not be what you want, but you can tweak that as needed. That is not my specialty. As is, this should create a basic HTML table that works in email. (I tested with Microsoft Outlook as my email client.)

     

    BTW, you did a nice job of extracting the details from each alarm message, but I think we could come up with something more efficient, possibly just a single. I'll see if I can make that work.



  • 4.  Re: Create Formatted table in HTML e-mail from Lua for Disk Usage

    Posted Mar 13, 2014 12:54 AM

    Here is a way to shorten the loop substantially and (probably) make it more efficient:

     

    --------------------------------------------------------------------------------
    -- BEFORE the for loop
    --------------------------------------------------------------------------------
    
    PATTERN = "disk free on (.+) is now (%d+)%%, which is below .+ threshold %((%d+)%%%).+ total size (.+)$"
    
    --------------------------------------------------------------------------------
    -- REPLACE the entire for loop
    --------------------------------------------------------------------------------
    
    for i,a in pairs(al) do
       disk,free,threshold,size = string.match(a.message, PATTERN)
    
       if disk ~= nil then
          html = html..sprintf(ROW_FORMAT, a.hostname, disk, free, threshold, size)
       end
    end

    The way you are doing it is completely legitimate, so you do not have to change your code if you are more comfortable with the existing approach. Although pattern matching with captures can be confusing, I think this shorter approach might be simpler too because you can see the bulk of the alarm message within the pattern.

     

    In our environment we use MB rather than % for disk thresholds in many cases. I was able to get the script to work with both kinds of alarms by making these changes to the format and the pattern before the loop:

     

    ROW_FORMAT =  -- multi-line quote...
    [[
      <TR>
        <TD>%s</TD>
        <TD>%s</TD>
        <TD>%s</TD>
        <TD>%s</TD>
        <TD>%s</TD>
      </TR>
    ]]
    
    PATTERN = "disk free on (.+) is now (%d+[^,]+), which is below .+ threshold %((%d+[^%)]+)%).+ total size (.+)$"

     



  • 5.  Re: Create Formatted table in HTML e-mail from Lua for Disk Usage

    Posted Mar 13, 2014 06:34 PM

    Keith, Thank you very much,  you are a god send.  lol.  Your script changes worked for my e-mail formatting and it looks incredible.  My manager is going to be very pleased with the format of the e-mails.  Thank you again.