Skip to content

Instantly share code, notes, and snippets.

@aleozlx
Last active September 16, 2023 10:36
Show Gist options
  • Save aleozlx/781dd2e79661c82aa080d99e67a703b6 to your computer and use it in GitHub Desktop.
Save aleozlx/781dd2e79661c82aa080d99e67a703b6 to your computer and use it in GitHub Desktop.

Revisions

  1. aleozlx revised this gist Apr 21, 2018. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions i3status-gpu.py
    Original file line number Diff line number Diff line change
    @@ -14,9 +14,6 @@
    import json

    def gpu_info():
    """ Get the current governor for cpu0, assuming all CPUs use the same. """
    # with open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor') as fp:
    # return fp.readlines()[0].strip()
    return subprocess.getoutput('nvidia-smi --query-gpu=gpu_name,pstate,utilization.gpu,utilization.memory,temperature.gpu --format=csv,noheader').strip()

    def print_line(message):
  2. aleozlx created this gist Apr 21, 2018.
    62 changes: 62 additions & 0 deletions i3status-gpu.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-

    # ref: https://github.com/i3/i3status/blob/master/contrib/wrapper.py
    #
    # To use it, ensure your ~/.i3status.conf contains this line:
    # output_format = "i3bar"
    # in the 'general' section.
    # Then, in your ~/.i3/config, use:
    # status_command i3status | ~/i3status/contrib/wrapper.py
    # In the 'bar' section.

    import sys, subprocess
    import json

    def gpu_info():
    """ Get the current governor for cpu0, assuming all CPUs use the same. """
    # with open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor') as fp:
    # return fp.readlines()[0].strip()
    return subprocess.getoutput('nvidia-smi --query-gpu=gpu_name,pstate,utilization.gpu,utilization.memory,temperature.gpu --format=csv,noheader').strip()

    def print_line(message):
    """ Non-buffered printing to stdout. """
    sys.stdout.write(message + '\n')
    sys.stdout.flush()

    def read_line():
    """ Interrupted respecting reader for stdin. """
    # try reading a line, removing any extra whitespace
    try:
    line = sys.stdin.readline().strip()
    # i3status sends EOF, or an empty line
    if not line:
    sys.exit(3)
    return line
    # exit on ctrl-c
    except KeyboardInterrupt:
    sys.exit()

    if __name__ == '__main__':
    # Skip the first line which contains the version header.
    print_line(read_line())

    # The second line contains the start of the infinite array.
    print_line(read_line())

    while True:
    line, prefix = read_line(), ''
    # ignore comma at start of lines
    if line.startswith(','):
    line, prefix = line[1:], ','

    j = json.loads(line)
    # insert information into the start of the json, but could be anywhere
    # CHANGE THIS LINE TO INSERT SOMETHING ELSE
    j.insert(0, {
    'full_text': gpu_info().strip().replace(',', '') + ' °C',
    'name': 'gpu',
    'color': '#3A85FF'
    })
    # and echo back new encoded json
    print_line(prefix+json.dumps(j))