-
Notifications
You must be signed in to change notification settings - Fork 3
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
hardikhdholariya
merged 7 commits into
CY-500-implement-device-inventoy-v2
from
add-user-inventory-dashboard
Sep 12, 2023
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5531b07
Changes in device inventory
hardikhdholariya 4349afb
Added user inventory logic and related macros & searches
hardikhdholariya 81cbb8d
Added user inventory dashboard
hardikhdholariya 6ac0ddf
Added macro to the cyences setup page
hardikhdholariya 997efc2
Remove commented code
hardikhdholariya 0845234
Updated the macro description
hardikhdholariya 1dcc3d2
Updated panel visualization from bar to column chart
hardikhdholariya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
cyences_app_for_splunk/appserver/static/js/build/cs_configuration.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
# 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__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
""" | ||
|
||
|
@@ -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 | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this comments.