summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMario Izquierdo (mariodebian) <mariodebian@gmail.com>2010-09-14 11:58:02 +0200
committerMario Izquierdo (mariodebian) <mariodebian@gmail.com>2010-09-14 11:58:02 +0200
commitcfef6d1a4b59f30399e7b79e5a08c95df7ab5322 (patch)
tree7b989facefbfc7deb78876020dd6fa26ab8cb713
parent21295a30ab85612af2789cae6622f4e8cc630816 (diff)
tcosmonitor/LocalData.py tcosmonitor/Sessions.py: use GDM and ConsoleKit dbus to read users and time (instead of wtmp)
-rw-r--r--debian/control1
-rw-r--r--debian/tcosmonitor-common.install1
-rw-r--r--tcosmonitor/LocalData.py55
-rw-r--r--tcosmonitor/Sessions.py232
4 files changed, 275 insertions, 14 deletions
diff --git a/debian/control b/debian/control
index 9e3e37d..3bfda45 100644
--- a/debian/control
+++ b/debian/control
@@ -37,6 +37,7 @@ Depends: python,
python-netifaces,
python-notify,
python-eggtrayicon | python-gnome2-extras,
+ python-dateutil,
zenity,
dbus,
x11vnc,
diff --git a/debian/tcosmonitor-common.install b/debian/tcosmonitor-common.install
index 415e350..6ba4ee5 100644
--- a/debian/tcosmonitor-common.install
+++ b/debian/tcosmonitor-common.install
@@ -7,6 +7,7 @@ debian/tmp/usr/lib/python*/*/tcosmonitor/TcosConf.py
debian/tmp/usr/lib/python*/*/tcosmonitor/TcosDBus.py
debian/tmp/usr/lib/python*/*/tcosmonitor/ping.py
debian/tmp/usr/lib/python*/*/tcosmonitor/shared.py
+debian/tmp/usr/lib/python*/*/tcosmonitor/Sessions.py
debian/tmp/usr/lib/python*/*/tcosmonitor/TcosCommon.py
debian/tmp/usr/lib/python*/*/tcosmonitor/LocalData.py
debian/tmp/usr/lib/python*/*/tcosmonitor/TcosXauth.py
diff --git a/tcosmonitor/LocalData.py b/tcosmonitor/LocalData.py
index 643c8d3..b64e8aa 100644
--- a/tcosmonitor/LocalData.py
+++ b/tcosmonitor/LocalData.py
@@ -469,6 +469,23 @@ class LocalData:
else:
return username
+ def isLastExclude(self, username, ingroup=None):
+ print_debug("isExclude() username=%s ingroup=%s "%(username, ingroup) )
+ exclude="noexclude"
+ if ingroup != None:
+ try:
+ cmd="groups %s 2>/dev/null" %username
+ usersingroup=[]
+ usersingroup=self.main.common.exe_cmd(cmd).split()
+ #usersingroup=grp.getgrnam(ingroup)[3]
+ except Exception, err:
+ usersingroup=[]
+ print_debug("usersingroup: %s" %usersingroup)
+ for group in usersingroup:
+ if ingroup == group:
+ exclude="exclude"
+ return exclude
+
def GetLast(self, ip, ingroup=None):
start=time()
last=None
@@ -478,6 +495,28 @@ class LocalData:
hostname=self.GetHostname(ip)
print_debug("GetLast() ip=%s hostname=%s "%(ip, hostname) )
+ # try to connect with GDM througth dbus to read all
+ # sessions & display info, better than read wtmp
+ import tcosmonitor.Sessions
+ try:
+ app=tcosmonitor.Sessions.Sessions()
+ for session in app.sessions:
+ if session.remote_host_name == ip:
+ print_debug("GetLast() session=%s"%session)
+ crono(start, "GetLast()")
+ data= {"pid":0,
+ "user":session.user,
+ "host":tcosmonitor.shared.parseIPAddress(session.remote_host_name),
+ "time":session.since,
+ "timelogged":session.diff,
+ "exclude":self.isLastExclude(session.user, ingroup)}
+ return data
+ except Exception, err:
+ print_debug("GetLast() Exception, no DBUS Session support, old GDM, err='%s'"%err)
+ #import traceback
+ #traceback.print_exc(file=sys.stderr)
+
+
for i in range(10):
last_file=WTMP_FILE
if i != 0:
@@ -535,22 +574,10 @@ class LocalData:
else:
timelogged="%dd %02dh:%02dm"%(days,hours,minutes)
- exclude="noexclude"
- if ingroup != None:
- try:
- cmd="groups %s 2>/dev/null" %last.ut_user
- usersingroup=[]
- usersingroup=self.main.common.exe_cmd(cmd).split()
- #usersingroup=grp.getgrnam(ingroup)[3]
- except Exception, err:
- usersingroup=[]
- print_debug("usersingroup: %s" %usersingroup)
- for group in usersingroup:
- if ingroup == group:
- exclude="exclude"
+ exclude=self.isLastExclude(last.ut_user, ingroup)
data={"pid":last.ut_pid, "user":last.ut_user, "host":last.ut_host.split(":")[0], "time":last.ut_tv[0], "timelogged":timelogged, "exclude":exclude}
- print_debug("IsLast() data=%s"%data)
+ print_debug("GetLast() data=%s"%data)
crono(start, "GetLast()")
return data
diff --git a/tcosmonitor/Sessions.py b/tcosmonitor/Sessions.py
new file mode 100644
index 0000000..9254b88
--- /dev/null
+++ b/tcosmonitor/Sessions.py
@@ -0,0 +1,232 @@
+# -*- coding: UTF-8 -*-
+##########################################################################
+# TcosMonitor writen by MarioDebian <mariodebian@gmail.com>
+#
+# TcosMonitor version __VERSION__
+#
+# Copyright (c) 2006 Mario Izquierdo <mariodebian@gmail.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+# 02111-1307, USA.
+###########################################################################
+
+import pwd
+import os
+import time
+import datetime
+import gzip
+
+import dbus
+import dateutil.parser
+
+CK_PATH="/var/log/ConsoleKit/"
+
+
+def username(uid):
+ try:
+ return pwd.getpwuid( int(uid) ).pw_name
+ except Exception, err:
+ return None
+
+class Connection(object):
+ def __repr__(self):
+ return "<Connection '%s': %s>"%(self.session_x11_display, str(self.__dict__))
+
+ def __init__(self, line):
+ self.type=None
+ self.seat_id=None
+ self.session_type=None
+ self.session_remote_host_name=None
+ self.session_unix_user=None
+ self.username=None
+ self.session_creation_time=None
+ self.session_x11_display=None
+ self.time=None
+ self.strtime=None
+ self.diffint=0
+ self.__parse__(line)
+ if self.session_unix_user:
+ self.username=username(self.session_unix_user)
+ if self.session_x11_display:
+ self.session_x11_display=self.session_x11_display.replace("'", "")
+ self.diff=self.diffnow()
+
+ def __parse__(self, line):
+ for elem in line.split():
+ if not "=" in elem and "." in elem:
+ self.strtime=elem
+ self.time=time.localtime( float(elem) )
+ continue
+ if not "=" in elem: continue
+ #print elem
+ varname=elem.split('=')[0].replace('-', '_')
+ value=elem.split('=')[1]
+ if hasattr(self, varname):
+ setattr(self, varname, value)
+
+ def diffnow(self):
+ diff=datetime.timedelta(0, time.time() - float(self.strtime))
+ """
+ >>> d = timedelta(microseconds=-1)
+ >>> (d.days, d.seconds, d.microseconds)
+ (-1, 86399, 999999)
+ """
+ self.diffint=int(diff.days)*86400 + int(diff.seconds)
+ if diff.days > 0:
+ return "%dd %s"%(diff.days, datetime.timedelta(0, diff.seconds))
+ else:
+ return "%s"%datetime.timedelta(0, diff.seconds)
+
+
+
+class ConsoleKitHistory(object):
+ def __init__(self, username=None, last=False):
+ self.logfiles=[]
+ self.data=[]
+ self.searchlogs()
+ # reverse logs
+ #self.logfiles.reverse()
+ #print self.logfiles
+ self.readlogs()
+ if username:
+ newdata=[]
+ for con in self.data:
+ if con.username == username:
+ newdata.append(con)
+ self.data=newdata
+
+ # sort array by diffint
+ self.data=sorted(self.data, key=lambda data: data.diffint)
+
+ if last:
+ newdata=[self.data[0]]
+ self.data=newdata
+
+ def readlogs(self):
+ for logfile in self.logfiles:
+ if ".gz" in logfile:
+ f = gzip.open(logfile, 'rb')
+ else:
+ f=open(logfile, 'r')
+ for line in f.readlines():
+ if "type=SEAT_SESSION_ADDED" in line:
+ con=Connection(line)
+ if con.username and int(con.session_unix_user) > 500 and con.session_x11_display != '' :
+ self.data.append( con )
+ f.close()
+
+ def searchlogs(self):
+ if os.path.isfile(CK_PATH + "history"):
+ self.logfiles.append(CK_PATH + "history")
+ i=1
+ if os.path.isfile(CK_PATH + "history" + "." + str(i)):
+ self.logfiles.append(CK_PATH + "history" + "." + str(i))
+ for i in range(2,10):
+ if os.path.isfile(CK_PATH + "history" + "." + str(i) + ".gz"):
+ self.logfiles.append(CK_PATH + "history" + "." + str(i) + ".gz")
+
+
+class Display(object):
+ def __repr__(self):
+ return "<Display '%s': %s>"%(self.x11_display, str(self.__dict__))
+
+ def __init__(self, obj):
+ self.id=None
+ self.seatid=None
+ self.active=False
+ self.is_local=None
+
+ self.remote_host_name=None
+ self.x11_display=None
+
+ self.unix_user=None
+ self.user=None
+ self.__parse__(obj)
+
+ def __parse__(self, obj):
+ self.id=str(obj.GetId())
+ self.seatid=str(obj.GetSeatId())
+
+ self.is_local=bool(obj.IsLocal())
+
+ self.remote_host_name=str(obj.GetRemoteHostname())
+ self.x11_display=str(obj.GetX11DisplayName())
+
+
+ bus = dbus.SystemBus ()
+ manager_obj = bus.get_object ('org.freedesktop.ConsoleKit', '/org/freedesktop/ConsoleKit/Manager')
+ manager = dbus.Interface (manager_obj, 'org.freedesktop.ConsoleKit.Manager')
+
+ for sessionid in manager.GetSessions():
+ session_obj = bus.get_object ('org.freedesktop.ConsoleKit', sessionid)
+ session = dbus.Interface (session_obj, 'org.freedesktop.ConsoleKit.Session')
+ if session.GetX11Display() == self.x11_display:
+
+ self.unix_user=int(session.GetUnixUser())
+ if self.unix_user > 900:
+ self.user=username(self.unix_user)
+
+ self.since= str(session.GetCreationTime())
+ self.active=True
+ self.diff=self.diffnow(self.since)
+ return
+
+ def diffnow(self, date):
+ diff=datetime.timedelta(0, time.mktime(time.gmtime()) - time.mktime(dateutil.parser.parse(date).timetuple()))
+ """
+ if days == 0:
+ timelogged="%02dh:%02dm"%(hours,minutes)
+ else:
+ timelogged="%dd %02dh:%02dm"%(days,hours,minutes)
+ """
+ if diff.days > 0:
+ return "%dd %s"%(diff.days, datetime.timedelta(0, diff.seconds))
+ else:
+ return "%s"%datetime.timedelta(0, diff.seconds)
+
+class Sessions(object):
+ def __init__(self):
+ self.sessions=[]
+ self.__get_all__()
+ pass
+
+ def __get_all__(self):
+ bus = dbus.SystemBus ()
+ manager_obj = bus.get_object ('org.gnome.DisplayManager', '/org/gnome/DisplayManager/Manager')
+ manager = dbus.Interface (manager_obj, 'org.gnome.DisplayManager.Manager')
+
+ sessions=[]
+ for display in manager.GetDisplays():
+ display_obj = bus.get_object ('org.gnome.DisplayManager', display)
+ session = dbus.Interface (display_obj, 'org.gnome.DisplayManager.Display')
+ self.sessions.append( Display(session) )
+
+
+
+if __name__ == "__main__":
+ # search for last connection of user prueba
+# app=ConsoleKitHistory('prueba', last=True)
+# for con in app.data:
+# print con
+# print "\n"
+
+
+ print "\n------------------------------\n"
+
+ # list all connections
+ app=Sessions()
+ for session in app.sessions:
+ print(session)
+ print "\n"