#11 Initial import
Merged 2 years ago by ankursinha. Opened 2 years ago by alciregi.
Unknown source master  into  master

Initial import
alciregi • 2 years ago  
@@ -0,0 +1,22 @@

+ Check FAS user status.

+ 

+ == Configuration:

+ rename `myconfig.cfg.example` to `myconfig.cfg` and edit it accordingly (it contains your FAS credentials).

+ 

+ == Syntax:

+ `python3 check.py username days`

+ 

+ Where `username` is the FAS user to check and `days` is the number of days to check on datagrepper in order to

+ get a list of activities logged from the fedmsg bus.

+ 

+ == Results

+ The script output is as follow:

+ - Username

+ - Human name

+ - Last seen (date of last FAS login)

+ - Groups

+  - list of FAS group membership, status and approval date

+  - total number of groups the user is member of

+ - Activities in the last X days:

+  - a list of activity topics grabbed from datagrepper

+  - total number of activities

@@ -0,0 +1,94 @@

+ import time

+ import datetime

+ import dateutil.relativedelta

+ import configparser

+ import requests

+ from fedora.client.fas2 import AccountSystem

+ import sys

+ 

+ configfile = 'myconfig.cfg'

+ 

+ config = configparser.ConfigParser()

+ config.read(configfile)

+ options = config.options('FAS')

+ userdata = {}

+ 

+ for opt in options:

+     userdata[opt] = config.get('FAS', opt)

+ 

+ user = userdata['user']

+ password = userdata['pass']

+ 

+ fas = AccountSystem(username=user, password=password)

+ fas.timeout = 600

+ fas.retries = 3

+ 

+ activity_baseurl = 'https://apps.fedoraproject.org/datagrepper/raw'

+ 

+ 

+ def getactivitycount(username,days):

+     days = int(days)

+     today = datetime.date.today()

+     d = datetime.datetime.strptime(today.strftime("%Y-%m-%d %H:%M:%S"), "%Y-%m-%d %H:%M:%S")

+     d = d - dateutil.relativedelta.relativedelta(days=days)

+     start = time.mktime(datetime.datetime.strptime(str(d), "%Y-%m-%d %H:%M:%S").timetuple())

+ 

+     activity_params = {'rows_per_page': 100, 'size': 'small', 'start': start, 'user': username}

+ 

+     activity_results = requests.get(activity_baseurl, params=activity_params).json()

+ 

+     pages = activity_results['pages']

+ 

+     for i in range(0, pages):

+         # print("aaaaaaaaaaaaaaaaaa",i)

+         activity_params2 = {'page': 1, 'rows_per_page': 100, 'size': 'small', 'start': start, 'user': username}

+         activity_results2 = requests.get(activity_baseurl, params=activity_params2).json()

+ 

+         for j in range(0, len(activity_results2['raw_messages'])):

+             # print(j,len(activity_results2['raw_messages']))

+             msgdate = datetime.datetime.fromtimestamp(int(activity_results2['raw_messages'][j]['timestamp']))

+             topic = activity_results2['raw_messages'][j]['topic']

+ 

+             print("\t", msgdate, topic)

+ 

+     return [activity_results['total'], d]

+ 

+ 

+ class Groups:

+ 

+     def __init__(self, groups):

+         self.count = 0

+         self.role_status = "na"

+         for group in groups:

+ 

+             # self.role_status = groups[group]['role_status']

+             # self.approval = groups[group]['approval']

+             print("\t", group, groups[group]['role_status'], groups[group]['approval'])

+             self.count += 1

+ 

+     def count_groups(self):

+         return self.count

+ 

+ 

+ def main(username,days):

+ 

+     print("Username:", username)

+     fas_user = fas.person_by_username(username)

+     print("Human name:", fas_user['human_name'])

+     last_seen = datetime.datetime.strptime(fas_user['last_seen'], "%Y-%m-%d %H:%M:%S.%f%z").strftime(

+         "%Y-%m-%d %H:%M:%S")

+     print("Last seen:", last_seen)

+     print("Groups:")

+     count_groups = Groups(fas_user['group_roles']).count_groups()

+     print("Total groups:", count_groups)

+     print("Activities:")

+     activities = getactivitycount(username, days)

+     date = datetime.datetime.strptime(str(activities[1]), "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d")

+     print("Total activities since {}: {}".format(date, activities[0]))

+ 

+ 

+ if __name__ == '__main__':

+     usernametocheck = sys.argv[1]

+     lastactivitiessincedays = sys.argv[2]

+     main(usernametocheck, lastactivitiessincedays)

+     print("End") 

\ No newline at end of file

@@ -0,0 +1,3 @@

+ [FAS]

+ user: fasuser

+ pass: faspassword 

\ No newline at end of file

A script that queries FAS and datagrepper in order to get some information about user activities.

Pull-Request has been merged by ankursinha

2 years ago