Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Anastasia Galkin
check-wp
Commits
fb3dc1a5
Commit
fb3dc1a5
authored
Aug 21, 2017
by
E-Science Admin
Browse files
Date added, headers changed to MIME
parent
e65002dd
Changes
1
Hide whitespace changes
Inline
Side-by-side
check-wp.py
View file @
fb3dc1a5
#!/usr/bin/python
import
time
import
MySQLdb
import
logging
import
smtplib
import
socket
import
time
from
email.mime.text
import
MIMEText
from
email.utils
import
formatdate
from
settings
import
*
log_name
=
''
+
time
.
strftime
(
"%Y%m%d_%H%M%S"
)
+
'_wordpress_stats.log'
logfile
=
open
(
log_name
,
"w"
)
# logger
logger
=
logging
.
getLogger
(
'check-wp'
)
hdlr
=
logging
.
FileHandler
(
LOG_DIR
+
LOG_FILE
)
formatter
=
logging
.
Formatter
(
'%(asctime)s - %(levelname)s - %(message)s'
)
hdlr
.
setFormatter
(
formatter
)
logger
.
addHandler
(
hdlr
)
logger
.
setLevel
(
logging
.
INFO
)
# email payload
msg_body
=
EMAIL_CONF
[
'FROM'
]
+
':
\n
'
# Open database connection
for
i
,
wp
in
WORDPRESS_DATABASES
.
items
():
for
i
nstance
,
wp
in
WORDPRESS_DATABASES
.
items
():
logfile
.
write
(
"Wordpress: '
{0}
'
"
.
format
(
i
)
+
'
\n
'
)
db_name
=
"
{0} "
.
format
(
wp
[
'NAME'
]
)
nonews
=
True
news
=
''
try
:
if
not
wp
[
'PORT'
]:
db_port
=
3306
else
:
db_port
=
int
(
wp
[
'PORT'
])
db
=
MySQLdb
.
connect
(
wp
[
'HOST'
],
wp
[
'USER'
],
wp
[
'PASSWORD'
],
wp
[
'DATABASE'
],
port
=
db_port
)
cursor
=
db
.
cursor
()
# users, posts, pages
for
queries
,
stats
in
WORDPRESS_STATS
.
items
():
cursor
.
execute
(
stats
[
'count'
])
count
=
cursor
.
fetchone
()
if
int
(
count
[
0
])
>
0
:
db
=
MySQLdb
.
connect
(
wp
[
'HOST'
],
wp
[
'USER'
],
wp
[
'PASSWORD'
],
wp
[
'DATABASE'
])
cursor
=
db
.
cursor
()
if
nonews
:
news
=
'
\n
INFO: '
+
db_name
+
' '
+
wp
[
'URL'
]
+
'
\n
'
nonews
=
False
# users, posts, pages
for
j
,
stats
in
WORDPRESS_STATS
.
items
():
news
=
news
+
"{1}: {0}
\n
"
.
format
(
''
.
join
(
map
(
str
,
count
)),
queries
)
cursor
.
execute
(
stats
[
'count'
])
count
=
cursor
.
fetchone
()
cursor
.
execute
(
stats
[
'query'
])
for
row
in
cursor
:
news
=
news
+
', '
.
join
(
map
(
str
,
row
))
+
'
\n
'
logger
.
info
(
db_name
+
queries
+
' '
+
' '
.
join
(
map
(
str
,
row
)))
if
int
(
count
[
0
])
>
0
:
nonews
=
False
logfile
.
write
(
"{0} new {1}
\n
"
.
format
(
''
.
join
(
map
(
str
,
count
)),
j
))
logfile
.
write
(
stats
[
'header'
]
+
"
\n
"
)
cursor
.
execute
(
stats
[
'query'
])
db
.
close
()
for
row
in
cursor
:
logfile
.
write
(
' | '
.
join
(
map
(
str
,
row
))
+
'
\n
'
)
if
nonews
:
logger
.
debug
(
db_name
+
" checked"
)
if
nonews
:
logfile
.
write
(
' Nothing new here.'
)
except
MySQLdb
.
Error
as
err
:
news
=
'
\n
WARNING: '
+
db_name
+
err
.
__str__
()
+
'
\n
'
logger
.
warning
(
db_name
+
err
.
__str__
())
logfile
.
write
(
'
\n
'
)
msg_body
=
msg_body
+
news
db
.
close
()
# email
msg
=
MIMEText
(
msg_body
)
msg
[
'Subject'
]
=
EMAIL_CONF
[
'SUBJECT'
]
msg
[
'From'
]
=
EMAIL_CONF
[
'FROM'
]
msg
[
'To'
]
=
' ,'
.
join
(
EMAIL_CONF
[
'TO'
])
msg
[
'Date'
]
=
formatdate
(
localtime
=
True
)
logfile
.
close
()
try
:
server
=
smtplib
.
SMTP
(
EMAIL_CONF
[
'SMTP_SERVER'
],
EMAIL_CONF
[
'PORT'
])
server
.
sendmail
(
EMAIL_CONF
[
'FROM'
],
EMAIL_CONF
[
'TO'
],
msg
.
as_string
())
server
.
close
()
except
(
smtplib
.
SMTPException
,
KeyError
,
TypeError
,
socket
.
error
)
as
err
:
logger
.
error
(
'SMTP server: '
+
err
.
__str__
())
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment