#!/usr/bin/env python #------------------------------------------------------------------------------ # Copyright (c) Perforce Software, Inc., 2007-2015. All rights reserved # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1 Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE # SOFTWARE, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH # DAMAGE. #------------------------------------------------------------------------------ import sys import os import platform import re from subprocess import * import shutil case_sensitive = False if platform.system() == "Windows": p4="p4.exe" else: p4="/p4/1/bin/p4_1" def loadconfig(): global case_sensitive robots = set() servers = [] cfgfile = open("totalusers.cfg", "r") for line in cfgfile.readlines(): if (re.search("^#", line) or re.search("^\s*$", line)): continue if re.search("^case_sensitive", line): if re.match("^case_sensitive=1.*", line): case_sensitive = True log("DEBUG", "Operating in case sensitive mode.") else: log("DEBUG", "Operating in case insensitive mode.") if re.search("^ROBOT", line): robotuser = (re.match("^ROBOT\|(.*)", line).groups()[0]) if case_sensitive: log("DEBUG", "Robot user %s added." % (robotuser)) robots.add(robotuser) else: log("DEBUG", "Robot user %s added." % (robotuser.lower())) robots.add(robotuser.lower()) if re.search("^SERVER", line): server = (re.match("^SERVER\|(.*)\|(.*)", line).groups()) log("DEBUG", "Server %s added." % (server[0])) servers.append(server) if servers == []: log("ERROR", "No servers found in config file.") cfgfile.close() return servers, robots def log(msglevel="DEBUG", message=""): if msglevel == "ERROR": print(message) sys.exit(1) else: print(message) def runp4cmd(cmd): try: pipe = Popen(p4 + cmd, shell=True, stdin=PIPE, stdout=PIPE, universal_newlines=True) stdout, stderr = pipe.communicate() log("DEBUG", stdout) if pipe.returncode != 0: log("ERROR", "%s%s generated the following error: %s" % (p4, cmd, stderr)) else: return stdout except OSError as err: log("ERROR", "Execution failed: %s" % (err)) def process_servers(servers, robots): """ Each server line contains four components as follows: SERVER|port|user """ users = [] useremail = {} accesstimes = {} userserver = {} for server in servers: args = " -p %s -u %s " % (server[0], server[1]) runp4cmd(args + "users > users.txt") userfile = open( "users.txt", "r") for userline in userfile.readlines(): user = re.match("(.*) <(.*)> .*accessed (.*)", userline).groups() if case_sensitive: username = user[0] else: username = user[0].lower() if username not in users and username not in robots: users.append(username) if (user[1] != None): useremail[username] = user[1] else: useremail[username] = username accesstimes[username] = user[2] userserver[username] = server[0] userfile.close() os.remove("users.txt") count = 0 users.sort() totalusersfile = "totalusers.csv" totalusers = open(totalusersfile, "w") for user in users: totalusers.write("%s,%s,%s,%s\n" % (user, useremail[user], accesstimes[user], userserver[user])) count += 1 totalusers.close() print("\nTotal number of users: %s" % (count)) print("User list is in %s" % (totalusersfile)) if __name__ == "__main__": if len(sys.argv) > 1: print("This program doesn't accept any arguments. Just update the totalusers.cfg file " "and run the program.") servers, robots = loadconfig() process_servers(servers, robots) sys.exit(0)
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#8 | 26652 | Robert Cowham |
This is Tom's change: Introduced new 'Unsupported' directory to clarify that some files in the SDP are not officially supported. These files are samples for illustration, to provide examples, or are deprecated but not yet ready for removal from the package. The Maintenance and many SDP triggers have been moved under here, along with other SDP scripts and triggers. Added comments to p4_vars indicating that it should not be edited directly. Added reference to an optional site_global_vars file that, if it exists, will be sourced to provide global user settings without needing to edit p4_vars. As an exception to the refactoring, the totalusers.py Maintenance script will be moved to indicate that it is supported. Removed settings to support long-sunset P4Web from supported structure. Structure under new .../Unsupported folder is: Samples/bin Sample scripts. Samples/triggers Sample trigger scripts. Samples/triggers/tests Sample trigger script tests. Samples/broker Sample broker filter scripts. Deprecated/triggers Deprecated triggers. To Do in a subsequent change: Make corresponding doc changes. |
||
#7 | 26060 | C. Thomas Tyler |
This supercedes review-24715. Bringing over recent bug fixes and code clean up from Rusty Jackson's local branch. Adds an edge maintenance script to unload clients on edge servers. Automated setting up maintenance.cfg from mkdirs.cfg settings. Cleaned up sdputils.py - It had a logic bug, and fixed all scripts based on changes. == Merge Details == Merge strategy: All the fixes to Maintenance scripts were brought in, but not the changes to active them in crontab that would enable them. Rusty's original change had crontab changes to call maintenance scripts, and also calls to 'p4 unlock -x' as a workround to p4d bugs related to orphanged locks on edge servers. Those bugs are already fixed. Also, adding anything new to cron requires a test suite addition and doc updates. So changes to the template.crontab.combined were ignored. However, a new file was added to the Maintenace folder, sample_cron_entries.txt, to make it easy for folks to see those changes. Rusty's original change had mkdirs.sh changes to generate maintenance.cfg. This change was enhanced during the merge. Rather than overwriting maintenance.cfg (potentially blowing away configuration for other instances), a new section for the new instance is appended to maintenance.cfg when 'mkdirs.sh <instance>' is run. Future considerations: Before adding anything from Maintenance to cron, we need to do the following: * Have mkdirs.sh install the Maintenance folder to the as-deployed structure under /p4/common, i.e. to /p4/common/maintenance, for HMS management compliance. * Add tests and docs for each script called (directly or indirectly). |
||
#6 | 21951 | C. Thomas Tyler |
Removed hard-coding assumption of an Instance named 1. Now, one of two things must be true: P4BIN must be defined, as it would be if the SDP envrionment is loaded in the usual way (source p4_vars N), or else the 'p4' executable must be found in the path. |
||
#5 | 16638 | C. Thomas Tyler |
Routine merge down to dev from main using: p4 merge -b perforce_software-sdp-dev |
||
#4 | 16029 | C. Thomas Tyler |
Routine merge to dev from main using: p4 merge -b perforce_software-sdp-dev |
||
#3 | 11477 | Russell C. Jackson (Rusty) |
Updated to use /usr/bin/env python Added workshop header. Changed cfg to config. |
||
#2 | 11464 | Russell C. Jackson (Rusty) | Current maintenance scripts. | ||
#1 | 10638 | C. Thomas Tyler | Populate perforce_software-sdp-dev. | ||
//guest/perforce_software/sdp/main/Maintenance/totalusers.py | |||||
#1 | 10148 | C. Thomas Tyler | Promoted the Perforce Server Deployment Package to The Workshop. |