Posts: 342
Threads: 130
Joined: May 2020
Reputation:
0
(12.02.2021, 15:27) admin Wrote: Run this script to log alerts for the current month and post the result:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
date =
os.date (
'*t' )
date.month =
date.month +
1
date.day ,
date.hour ,
date.min ,
date.sec =
1 ,
0 ,
0 ,
0
ts_end =
os.time (
date )
date.month =
date.month -
1
ts_start =
os.time (
date )
alerts_table =
db :
getall (
'SELECT * FROM alerts where alerttime BETWEEN ? AND ? ORDER BY id DESC' ,
ts_start ,
ts_end )
buffer = {}
csv =
string.format (
'%q,%q,%q' ,
"ID" ,
"Messaggio" ,
"Data e Ora" )
table.insert (
buffer ,
csv )
table.insert (
buffer ,
"" )
for _ ,
alerts in ipairs (
alerts_table )
do
csv =
string.format (
'%q,%q,%q' ,
alerts.id ,
alerts.alert ,
os.date (
"%d.%m.%Y %X" ,
alerts.alerttime ))
table.insert (
buffer ,
csv )
end
res =
table.concat (
buffer ,
'\n' )
log (
res )
this is the result
Posts: 8106
Threads: 43
Joined: Jun 2015
Reputation:
471
This means that the export from DB to CSV part is working correctly. There must be something else wrong with your script if data is not populated.
Posts: 342
Threads: 130
Joined: May 2020
Reputation:
0
(12.02.2021, 15:41) admin Wrote: This means that the export from DB to CSV part is working correctly. There must be something else wrong with your script if data is not populated.
But if I use
alerts_table = db:getall('SELECT * FROM alerts ORDER BY id DESC')
the script working fine.
filtering is probably the problem
BR
Posts: 8106
Threads: 43
Joined: Jun 2015
Reputation:
471
Post the latest version of your script that should export current month data instead of previous
Posts: 342
Threads: 130
Joined: May 2020
Reputation:
0
(12.02.2021, 16:59) admin Wrote: Post the latest version of your script that should export current month data instead of previous
This i scomplete scritp for currento mounth
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
user =
'...'
password =
'...'
from =
'<' ..
user ..
'>'
alias_from =
'Do not Reply '
to =
'<claudio@ciemme.com>'
alias_to =
to
subjectpart1 =
'Lista Allarmi Mese'
subjectpart2 =
'E-Mail inviata in automatico dal WebServer'
epilogue =
'End of message'
date =
os.date (
'*t' )
date.day ,
date.hour ,
date.min ,
date.sec =
1 ,
0 ,
0 ,
0
ts_end =
os.time (
date )
date.month =
date.month +
1
ts_start =
os.time (
date )
alerts_table =
db :
getall (
'SELECT * FROM alerts where alerttime BETWEEN ? AND ? ORDER BY id DESC' ,
ts_start ,
ts_end )
log (
'Dal - ' ..
os.date (
"%F" ,
ts_start ) ..
' Al - ' ..
os.date (
"%F" ,
ts_end ))
buffer = {}
csv =
string.format (
'%q,%q,%q' ,
"ID" ,
"Messaggio" ,
"Data e Ora" )
table.insert (
buffer ,
csv )
table.insert (
buffer ,
"" )
for _ ,
alerts in ipairs (
alerts_table )
do
csv =
string.format (
'%q,%q,%q' ,
alerts.id ,
alerts.alert ,
os.date (
"%d.%m.%Y %X" ,
alerts.alerttime ))
table.insert (
buffer ,
csv )
end
local settings = {
from =
from ,
rcpt =
to ,
user =
user ,
password =
password ,
server =
'smtps.aruba.it' ,
port =
465 ,
secure =
'sslv23' ,
}
src =
os.date (
'%Y-%m' ) ..
'.csv'
dst =
'/home/ftp/' ..
src
io.writefile (
dst ,
buffer )
subject =
subjectpart1 ..
": " ..
src ..
" " ..
subjectpart2
local smtp =
require (
"socket.smtp" )
local mime =
require (
"mime" )
local ltn12 =
require (
"ltn12" )
settings.source =
smtp.message {
headers = {
from =
'' ..
alias_from ..
' ' ..
from ..
'' ,
to =
'' ..
alias_to ..
' ' ..
to ..
'' ,
subject =
subject
},
body = {
preamble =
"" ,
[
1 ] = {
headers = {
[
"content-type" ] =
'text/plain' ,
[
"content-disposition" ] =
'attachment; filename="' ..
src.. '"' ,
[
"content-description" ] =
'.. src ..' ,
[
"content-transfer-encoding" ] =
"BASE64" ,
},
body =
ltn12.source.chain (
ltn12.source.file (
io.open (
dst ,
"rb" )),
ltn12.filter.chain (
mime.encode (
"base64" ),
mime.wrap ()
)
)
},
epilogue =
epilogue
}
}
r ,
e =
smtp.send (
settings )
if (
e )
then
log (
e )
log (
r )
alert (
"Could not send email: " ,
e ,
"\n" )
end
os.remove (
dst )
BR
Posts: 8106
Threads: 43
Joined: Jun 2015
Reputation:
471
You've made the change incorrectly, now filter starting date is after the ending date. This should produce correct filter period:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
user =
'...'
password =
'...'
from =
'<' ..
user ..
'>'
alias_from =
'Do not Reply '
to =
'<claudio@ciemme.com>'
alias_to =
to
subjectpart1 =
'Lista Allarmi Mese'
subjectpart2 =
'E-Mail inviata in automatico dal WebServer'
epilogue =
'End of message'
date =
os.date (
'*t' )
date.day ,
date.hour ,
date.min ,
date.sec =
1 ,
0 ,
0 ,
0
ts_start =
os.time (
date )
date.month =
date.month +
1
ts_end =
os.time (
date )
alerts_table =
db :
getall (
'SELECT * FROM alerts where alerttime BETWEEN ? AND ? ORDER BY id DESC' ,
ts_start ,
ts_end )
log (
'Dal - ' ..
os.date (
"%F" ,
ts_start ) ..
' Al - ' ..
os.date (
"%F" ,
ts_end ))
buffer = {}
csv =
string.format (
'%q,%q,%q' ,
"ID" ,
"Messaggio" ,
"Data e Ora" )
table.insert (
buffer ,
csv )
table.insert (
buffer ,
"" )
for _ ,
alerts in ipairs (
alerts_table )
do
csv =
string.format (
'%q,%q,%q' ,
alerts.id ,
alerts.alert ,
os.date (
"%d.%m.%Y %X" ,
alerts.alerttime ))
table.insert (
buffer ,
csv )
end
local settings = {
from =
from ,
rcpt =
to ,
user =
user ,
password =
password ,
server =
'smtps.aruba.it' ,
port =
465 ,
secure =
'sslv23' ,
}
src =
os.date (
'%Y-%m' ) ..
'.csv'
dst =
'/home/ftp/' ..
src
io.writefile (
dst ,
buffer )
subject =
subjectpart1 ..
": " ..
src ..
" " ..
subjectpart2
local smtp =
require (
"socket.smtp" )
local mime =
require (
"mime" )
local ltn12 =
require (
"ltn12" )
settings.source =
smtp.message {
headers = {
from =
'' ..
alias_from ..
' ' ..
from ..
'' ,
to =
'' ..
alias_to ..
' ' ..
to ..
'' ,
subject =
subject
},
body = {
preamble =
"" ,
[
1 ] = {
headers = {
[
"content-type" ] =
'text/plain' ,
[
"content-disposition" ] =
'attachment; filename="' ..
src.. '"' ,
[
"content-description" ] =
'.. src ..' ,
[
"content-transfer-encoding" ] =
"BASE64" ,
},
body =
ltn12.source.chain (
ltn12.source.file (
io.open (
dst ,
"rb" )),
ltn12.filter.chain (
mime.encode (
"base64" ),
mime.wrap ()
)
)
},
epilogue =
epilogue
}
}
r ,
e =
smtp.send (
settings )
if (
e )
then
log (
e )
log (
r )
alert (
"Could not send email: " ,
e ,
"\n" )
end
os.remove (
dst )
Posts: 342
Threads: 130
Joined: May 2020
Reputation:
0
(15.02.2021, 07:49) admin Wrote: You've made the change incorrectly, now filter starting date is after the ending date. This should produce correct filter period:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
user =
'...'
password =
'...'
from =
'<' ..
user ..
'>'
alias_from =
'Do not Reply '
to =
'<claudio@ciemme.com>'
alias_to =
to
subjectpart1 =
'Lista Allarmi Mese'
subjectpart2 =
'E-Mail inviata in automatico dal WebServer'
epilogue =
'End of message'
date =
os.date (
'*t' )
date.day ,
date.hour ,
date.min ,
date.sec =
1 ,
0 ,
0 ,
0
ts_start =
os.time (
date )
date.month =
date.month +
1
ts_end =
os.time (
date )
alerts_table =
db :
getall (
'SELECT * FROM alerts where alerttime BETWEEN ? AND ? ORDER BY id DESC' ,
ts_start ,
ts_end )
log (
'Dal - ' ..
os.date (
"%F" ,
ts_start ) ..
' Al - ' ..
os.date (
"%F" ,
ts_end ))
buffer = {}
csv =
string.format (
'%q,%q,%q' ,
"ID" ,
"Messaggio" ,
"Data e Ora" )
table.insert (
buffer ,
csv )
table.insert (
buffer ,
"" )
for _ ,
alerts in ipairs (
alerts_table )
do
csv =
string.format (
'%q,%q,%q' ,
alerts.id ,
alerts.alert ,
os.date (
"%d.%m.%Y %X" ,
alerts.alerttime ))
table.insert (
buffer ,
csv )
end
local settings = {
from =
from ,
rcpt =
to ,
user =
user ,
password =
password ,
server =
'smtps.aruba.it' ,
port =
465 ,
secure =
'sslv23' ,
}
src =
os.date (
'%Y-%m' ) ..
'.csv'
dst =
'/home/ftp/' ..
src
io.writefile (
dst ,
buffer )
subject =
subjectpart1 ..
": " ..
src ..
" " ..
subjectpart2
local smtp =
require (
"socket.smtp" )
local mime =
require (
"mime" )
local ltn12 =
require (
"ltn12" )
settings.source =
smtp.message {
headers = {
from =
'' ..
alias_from ..
' ' ..
from ..
'' ,
to =
'' ..
alias_to ..
' ' ..
to ..
'' ,
subject =
subject
},
body = {
preamble =
"" ,
[
1 ] = {
headers = {
[
"content-type" ] =
'text/plain' ,
[
"content-disposition" ] =
'attachment; filename="' ..
src.. '"' ,
[
"content-description" ] =
'.. src ..' ,
[
"content-transfer-encoding" ] =
"BASE64" ,
},
body =
ltn12.source.chain (
ltn12.source.file (
io.open (
dst ,
"rb" )),
ltn12.filter.chain (
mime.encode (
"base64" ),
mime.wrap ()
)
)
},
epilogue =
epilogue
}
}
r ,
e =
smtp.send (
settings )
if (
e )
then
log (
e )
log (
r )
alert (
"Could not send email: " ,
e ,
"\n" )
end
os.remove (
dst )
perfect thank you very much