2014-10-01 16:42:40 +00:00
|
|
|
import os
|
2014-07-25 15:17:50 +00:00
|
|
|
import textwrap
|
|
|
|
|
2014-07-11 14:48:46 +00:00
|
|
|
from django.utils import timezone
|
2014-05-08 16:59:35 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2014-10-01 16:42:40 +00:00
|
|
|
from orchestra.apps.orchestration import ServiceController
|
|
|
|
from orchestra.apps.resources import ServiceMonitor
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-10-01 16:42:40 +00:00
|
|
|
from . import settings
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-10-01 16:42:40 +00:00
|
|
|
|
|
|
|
class SystemUserBackend(ServiceController):
|
2014-09-30 16:06:42 +00:00
|
|
|
verbose_name = _("System user")
|
|
|
|
model = 'systemusers.SystemUser'
|
2014-10-01 16:42:40 +00:00
|
|
|
|
|
|
|
def save(self, user):
|
|
|
|
context = self.get_context(user)
|
|
|
|
groups = ','.join(self.get_groups(user))
|
|
|
|
context['groups_arg'] = '--groups %s' % groups if groups else ''
|
|
|
|
self.append(textwrap.dedent("""
|
|
|
|
if [[ $( id %(username)s ) ]]; then
|
|
|
|
usermod %(username)s --password '%(password)s' --shell %(shell)s %(groups_arg)s
|
|
|
|
else
|
2014-10-02 15:58:27 +00:00
|
|
|
useradd %(username)s --home %(home)s --password '%(password)s' --shell %(shell)s %(groups_arg)s
|
2014-10-01 16:42:40 +00:00
|
|
|
usermod -a -G %(username)s %(mainusername)s
|
|
|
|
fi
|
|
|
|
mkdir -p %(home)s
|
|
|
|
chown %(username)s.%(username)s %(home)s""" % context
|
|
|
|
))
|
|
|
|
|
|
|
|
def delete(self, user):
|
|
|
|
context = self.get_context(user)
|
|
|
|
self.append("{ sleep 2 && killall -u %(username)s -s KILL; } &" % context)
|
|
|
|
self.append("killall -u %(username)s || true" % context)
|
|
|
|
self.append("userdel %(username)s || true" % context)
|
|
|
|
self.append("groupdel %(username)s || true" % context)
|
2014-10-17 10:04:47 +00:00
|
|
|
self.delete_home(context, user)
|
|
|
|
|
|
|
|
def delete_home(self, context, user):
|
2014-10-02 15:58:27 +00:00
|
|
|
if user.is_main:
|
|
|
|
# TODO delete instead of this shit
|
2014-10-07 13:08:59 +00:00
|
|
|
context['deleted'] = context['home'].rstrip('/') + '.deleted'
|
2014-10-02 15:58:27 +00:00
|
|
|
self.append("mv %(home)s %(deleted)s" % context)
|
2014-10-01 16:42:40 +00:00
|
|
|
|
|
|
|
def get_groups(self, user):
|
|
|
|
if user.is_main:
|
2014-10-02 15:58:27 +00:00
|
|
|
return user.account.systemusers.exclude(username=user.username).values_list('username', flat=True)
|
2014-10-01 16:42:40 +00:00
|
|
|
groups = list(user.groups.values_list('username', flat=True))
|
2014-10-24 14:19:34 +00:00
|
|
|
groups += list(settings.SYSTEMUSERS_DEFAULT_GROUP_MEMBERS)
|
2014-10-01 16:42:40 +00:00
|
|
|
return groups
|
|
|
|
|
|
|
|
def get_context(self, user):
|
|
|
|
context = {
|
|
|
|
'username': user.username,
|
|
|
|
'password': user.password if user.active else '*%s' % user.password,
|
|
|
|
'shell': user.shell,
|
|
|
|
'mainusername': user.username if user.is_main else user.account.username,
|
2014-10-02 15:58:27 +00:00
|
|
|
'home': user.get_home()
|
2014-10-01 16:42:40 +00:00
|
|
|
}
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserDisk(ServiceMonitor):
|
|
|
|
model = 'systemusers.SystemUser'
|
|
|
|
resource = ServiceMonitor.DISK
|
|
|
|
verbose_name = _('Main user disk')
|
|
|
|
|
|
|
|
def monitor(self, user):
|
|
|
|
context = self.get_context(user)
|
2014-10-27 13:29:02 +00:00
|
|
|
self.append("du -s %(home)s | cut -f1 | xargs echo %(object_id)s" % context)
|
2014-10-01 16:42:40 +00:00
|
|
|
|
|
|
|
def get_context(self, user):
|
|
|
|
context = SystemUserBackend().get_context(user)
|
|
|
|
context['object_id'] = user.pk
|
|
|
|
return context
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-07-09 16:17:43 +00:00
|
|
|
|
2014-10-01 16:42:40 +00:00
|
|
|
class FTPTraffic(ServiceMonitor):
|
2014-09-30 16:06:42 +00:00
|
|
|
model = 'systemusers.SystemUser'
|
2014-10-01 16:42:40 +00:00
|
|
|
resource = ServiceMonitor.TRAFFIC
|
|
|
|
verbose_name = _('Main FTP traffic')
|
|
|
|
|
|
|
|
def prepare(self):
|
|
|
|
current_date = timezone.localtime(self.current_date)
|
|
|
|
current_date = current_date.strftime("%Y%m%d%H%M%S")
|
|
|
|
self.append(textwrap.dedent("""
|
|
|
|
function monitor () {
|
|
|
|
OBJECT_ID=$1
|
|
|
|
INI_DATE=$2
|
|
|
|
USERNAME="$3"
|
|
|
|
LOG_FILE="$4"
|
|
|
|
grep "UPLOAD\|DOWNLOAD" "${LOG_FILE}" \\
|
|
|
|
| grep " \\[${USERNAME}\\] " \\
|
|
|
|
| awk -v ini="${INI_DATE}" '
|
|
|
|
BEGIN {
|
|
|
|
end = "%s"
|
|
|
|
sum = 0
|
|
|
|
months["Jan"] = "01"
|
|
|
|
months["Feb"] = "02"
|
|
|
|
months["Mar"] = "03"
|
|
|
|
months["Apr"] = "04"
|
|
|
|
months["May"] = "05"
|
|
|
|
months["Jun"] = "06"
|
|
|
|
months["Jul"] = "07"
|
|
|
|
months["Aug"] = "08"
|
|
|
|
months["Sep"] = "09"
|
|
|
|
months["Oct"] = "10"
|
|
|
|
months["Nov"] = "11"
|
|
|
|
months["Dec"] = "12"
|
|
|
|
} {
|
|
|
|
# log: Fri Jul 11 13:23:17 2014
|
|
|
|
split($4, t, ":")
|
|
|
|
# line_date = year month day hour minute second
|
|
|
|
line_date = $5 months[$2] $3 t[1] t[2] t[3]
|
|
|
|
if ( line_date > ini && line_date < end)
|
|
|
|
split($0, l, "\\", ")
|
|
|
|
split(l[3], b, " ")
|
|
|
|
sum += b[1]
|
|
|
|
} END {
|
|
|
|
print sum
|
|
|
|
}
|
|
|
|
' | xargs echo ${OBJECT_ID}
|
|
|
|
}""" % current_date))
|
|
|
|
|
|
|
|
def monitor(self, user):
|
|
|
|
context = self.get_context(user)
|
|
|
|
self.append(
|
|
|
|
'monitor %(object_id)i %(last_date)s "%(username)s" "%(log_file)s"' % context)
|
|
|
|
|
|
|
|
def get_context(self, user):
|
|
|
|
last_date = timezone.localtime(self.get_last_date(user.pk))
|
|
|
|
return {
|
|
|
|
'log_file': settings.SYSTEMUSERS_FTP_LOG_PATH,
|
|
|
|
'last_date': last_date.strftime("%Y%m%d%H%M%S"),
|
|
|
|
'object_id': user.pk,
|
|
|
|
'username': user.username,
|
|
|
|
}
|