Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user inventory dashboard #387

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
width: auto !important;
}

#row_expanded_table table tbody tr td {
border: 1px solid;
border-color: black;
}

/* Device Inventory Manager Row Expansion */
#row_expanded_table table thead tr th:not(:last-child) {
width: 50px;
Expand All @@ -17,4 +12,6 @@
#row_expanded_table table thead tr th,
#row_expanded_table table tbody tr td {
text-align: left !important;
border: 1px solid;
border-color: black;
}

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions cyences_app_for_splunk/bin/cyences_device_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
import logging
import logger_manager

logger = logger_manager.setup_logging("device_manager_v2", logging.INFO)
logger = logger_manager.setup_logging("device_inventory_v2", logging.INFO)

IS_DEBUGGING_MODE = False
CY_HOSTNAME_POSTFIXES_MACRO = "cs_device_inventory_hostname_postfixes"
DEVICE_INVENTORY_LOOKUP_COLLECTION = "cs_device_inventory_collection_v2"

Expand Down
131 changes: 131 additions & 0 deletions cyences_app_for_splunk/bin/cyences_user_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#!/usr/bin/env python

import sys
import time
import copy

from splunklib.searchcommands import dispatch, EventingCommand, Configuration, Option, validators
from user_inventory_v2_util import UserManager, UserEntry

import cs_utils
import logging
import logger_manager

logger = logger_manager.setup_logging("user_inventory", logging.INFO)

CY_USER_POSTFIXES_MACRO = "cs_user_inventory_user_postfixes"
USER_INVENTORY_LOOKUP_COLLECTION = "cs_user_inventory_collection"

MONTH_IN_SECOND = 60 * 60 * 24 * 30.5
YEAR_IN_SECOND = MONTH_IN_SECOND * 12
MAX_TIME_EPOCH = 2147483647 # Tue Jan 19 2038 03:14:07


@Configuration()
class CyencesUserManagerCommand(EventingCommand):
operation = Option(name="operation", require=False, default="getusers")

# user_postfixes = Option(name="user_postfixes", require=True) # You can put comma separated values like, ".ad.crossrealms.com, .crossrealms.com"
# Always put large string early if shorter string is subset of large string.
# for example, ".ad.crossrealms.com" should be before ".crossrealms.com"
# I prefer to also put ".local" at the end as well to ensure proper user matching

products_to_cleanup = Option(name="products_to_cleanup", require=False, default="*")
cleanup_minindextime = Option(name="minindextime", require=False, default=None, validate=validators.Float()) # default past 1 years
cleanup_maxindextime = Option(name="maxindextime", require=False, default=None, validate=validators.Float()) # default forseeable future
target_user = Option(name="target_user", require=False, default="")
users_to_merge = Option(name="users_to_merge", require=False, default=None)
# cleanup_ip_mintime = Option(name="ipmintime", require=False, default=None, validate=validators.Float()) # default past 30 days
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this comments.

# cleanup_ip_maxtime = Option(name="ipmaxtime", require=False, default=None, validate=validators.Float()) # default forseeable future

@staticmethod
def validate_param_value_and_type(command_options):
if command_options == "*":
command_options = None
elif command_options is None:
command_options = []
elif type(command_options) == str:
command_options = [
element.strip().strip('\'"')
for element in command_options.strip('\'"()').split(",")
if element.strip()
]
elif type(command_options) == list:
for element in command_options:
element.strip().strip('\'"')
else:
raise Exception("{} value is not as expected.".format(command_options))
return command_options

def validate_inputs(self):
if self.operation not in ["getusers", "addentries", "cleanup", "merge", "manualmerge"]:
raise Exception("operation - allowed values: getusers, addentries, cleanup, merge, manualmerge")

if self.operation == "cleanup":
timenow = time.time()

if self.cleanup_minindextime is None:
self.cleanup_minindextime = timenow - YEAR_IN_SECOND
if self.cleanup_maxindextime is None:
self.cleanup_maxindextime = MAX_TIME_EPOCH

if self.cleanup_minindextime >= self.cleanup_maxindextime:
raise Exception("minindextime should be less than maxindextime.")

self.products_to_cleanup = self.validate_param_value_and_type(self.products_to_cleanup)

elif self.operation == "manualmerge":
self.users_to_merge = self.validate_param_value_and_type(self.users_to_merge)

def transform(self, records):
self.validate_inputs()

session_key = cs_utils.GetSessionKey(logger).from_custom_command(self)

if self.operation == "getusers":
with UserManager(session_key, logger, USER_INVENTORY_LOOKUP_COLLECTION) as dm:
_users = dm.get_user_details()
for user in _users:
yield user

elif self.operation == "addentries":
conf_manger = cs_utils.ConfigHandler(logger, session_key)
user_postfixes = conf_manger.get_macro(CY_USER_POSTFIXES_MACRO)
with UserManager(session_key, logger, USER_INVENTORY_LOOKUP_COLLECTION, user_postfixes) as dm:
for record in records:
other_fields = copy.deepcopy(record)
del other_fields["indextime"]
del other_fields["vendor_product"]
del other_fields["user_type"]
del other_fields["index"]
del other_fields["sourcetype"]
del other_fields["user"]
entry = UserEntry(record["vendor_product"], record["indextime"], record["user"], record["index"], record["sourcetype"], record["user_type"], other_fields)
user_id = dm.add_user_entry(entry)
record["user_id"] = user_id
yield record

elif self.operation == "cleanup":
with UserManager(session_key, logger, USER_INVENTORY_LOOKUP_COLLECTION) as dm:
messages = dm.cleanup_users(self.cleanup_minindextime, self.cleanup_maxindextime, self.products_to_cleanup)
for m in messages:
yield {"message": m}

elif self.operation == "merge":
conf_manger = cs_utils.ConfigHandler(logger, session_key)
user_postfixes = conf_manger.get_macro(CY_USER_POSTFIXES_MACRO)
with UserManager(session_key, logger, USER_INVENTORY_LOOKUP_COLLECTION, user_postfixes) as dm:
messages = dm.reorganize_user_list()
for m in messages:
yield {"message": m}

elif self.operation == "manualmerge":
conf_manger = cs_utils.ConfigHandler(logger, session_key)
user_postfixes = conf_manger.get_macro(CY_USER_POSTFIXES_MACRO)
with UserManager(session_key, logger, USER_INVENTORY_LOOKUP_COLLECTION, user_postfixes) as dm:
messages = dm.manually_merge_users(self.target_user, *self.users_to_merge)
for m in messages:
yield {"message": m}


dispatch(CyencesUserManagerCommand, sys.argv, sys.stdin, sys.stdout, __name__)
4 changes: 2 additions & 2 deletions cyences_app_for_splunk/bin/device_inventory_v2_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


Dev Details
* It now stores everything inside a Python pickle file instead of a lookup
* It now stores everything inside a lookup.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inside a kvstore collection

* Most of the processing now happens inside the Python script.
"""

Expand All @@ -27,7 +27,7 @@

def remove_words_from_end(sentence, words):
for word in words:
if sentence.endswith(word):
if sentence.endswith(word.lower()):
sentence = sentence[: -len(word)]
return sentence

Expand Down
Loading
Loading