-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Implement status.uptime
on macOS
#37157
Conversation
9ad0341
to
ae687a4
Compare
Go Go Jenkins! |
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.
@jfindlay please look at these.
elif salt.utils.is_sunos(): | ||
cmd = "kstat -p unix:0:system_misc:boot_time | nawk '{printf \"%d\\n\", srand()-$2}'" | ||
ut_ret['seconds'] = int(__salt__['cmd.shell'](cmd, output_loglevel='trace').strip() or 0) | ||
seconds = float(__salt__['cmd.shell'](cmd, output_loglevel='trace').strip() or 0) |
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.
On Solaris, with kstat
you will always get an integer, no need to float
here.
raise CommandExecutionError('Cannot find kern.boottime system parameter') | ||
sec_data, usec_data = bt_data.split('}')[0].strip(' {').split(', ') | ||
sec = sec_data.split('sec = ')[1] | ||
usec = usec_data.split('usec = ')[1] |
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.
I would not do that and suggest something more reliable, although probably less readable:
data = bt_data.split("{")[-1].split("}")[0].strip().replace(' ', '')
uptime = dict([(k_sec, int(v_sec,))
for k_sec, v_sec in [pair.strip().split("=")
for pair in data.split(",")]])
What you do here is to turn mac returned data into a dictionary, and refer to it something like:
uptime['usec']
uptime['sec']
You can turn the above to a lambda and also query sleep time etc.
P.S. It can be oneliner, but then @thatch45 will hate me for this. 😆
sec_data, usec_data = bt_data.split('}')[0].strip(' {').split(', ') | ||
sec = sec_data.split('sec = ')[1] | ||
usec = usec_data.split('usec = ')[1] | ||
seconds = curr_seconds - float(sec + '.' + '{0:0>6}'.format(usec)) |
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.
BTW, Mac is also only an integer, no floats there. 😃
@cachedout please hold a moment. 😄 |
@isbm Will do. :] |
@jfindlay two more to go. :-) |
@jfindlay Could you please let me know where you are with this one? |
Because there has been no response, I am closing this. @jfindlay when you are ready to go here please re-open it. |
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.
@jfindlay Thanks for the update! Could you please still look at this one?
if not bt_data: | ||
raise CommandExecutionError('Cannot find kern.boottime system parameter') | ||
sec_data, usec_data = bt_data.split('}')[0].strip(' {').split(', ') | ||
seconds = int(curr_seconds - int(sec_data.split('sec = ')[1])) |
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.
@jfindlay If you could use my previous suggestion, we by now would also have sleep time of Mac!
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.
Specifically:
data = bt_data.split("{")[-1].split("}")[0].strip().replace(' ', '')
uptime = dict([(k_sec, int(v_sec,))
for k_sec, v_sec in [pair.strip().split("=")
for pair in data.split(",")]])
What you do here is to turn mac returned data into a dictionary, and refer to it something like:
uptime['usec']
uptime['sec']
You can turn the above to a lambda and also query sleep time etc.
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.
I'm not sure what you want me to do.
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.
Point is, you could reuse it and get also other values from the sysctl
, such as "sleeptime" or "waketime". That said, you could turn the code above to a def _internal_function(data)
and reuse it.
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.
@isbm, you're welcome to open a pull request on my branch, https://github.com/jfindlay/salt/compare/mac_time...isbm:mac_time
.
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.
Ah, but that's easy:
def _parse_sysctl(data):
data = data.split("{")[-1].split("}")[0].strip().replace(' ', '')
return dict([(k_sec, int(v_sec,))
for k_sec, v_sec in [pair.strip().split("=")
for pair in data.split(",")]])
...
seconds = int(curr_seconds - _parse_sysctl(bt_data)['sec'])
At the moment I don't have Salt installed on OSX.
@cachedout, none from me. Excellent job everyone. |
What does this PR do?
Implement
status.uptime
on macOSWhat issues does this PR fix or reference?
#37158
Tests written?
Yes