0

I'm trying to deploy the project from VSCode to Azure function app that has Linux operating system. I run the project locally on Windows using processor id by using following command:

os.popen("wmic cpu get processorid").read().replace("\n", "").replace("  ", "").replace(" ", "")[11:]

I need the analogue of the aforementioned command to get processor id for Linux system. I tried below command on Ubuntu terminal and it worked:

sudo dmidecode --type processor

enter image description here

I get errors when I write it in VSCode and deploy it to Azure function app using following code (from [here]):

import platform, subprocess, logging

def get_processor_info():
    if platform.system() == "Windows":
        return platform.processor()
    elif platform.system() == "Darwin":
        return subprocess.check_output(['/usr/sbin/sysctl', "-n", "machdep.cpu.brand_string"]).strip()
    elif platform.system() == "Linux":
        command = "sudo dmidecode --type processor"
        logging.info("My command: %s.", str(subprocess.check_output(command, shell=True).strip()))
    return ""
    
get_processor_info()

Output of the error is: " Result: Failure Exception: CalledProcessError: Command '['sudo', 'dmidecode', '--type', 'processor']' returned non-zero exit status 1. Stack: File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 405, in _handle__invocation_request invocation_id, fi_context, fi.func, args) File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 612, in _run_sync_func func)(params) File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/extension.py", line 215, in _raw_invocation_wrapper result = function(**args) File "/home/site/wwwroot/timer_trig_05/init.py", line 79, in main logging.info("My command: %s.", str(get_processor_info())) File "/home/site/wwwroot/timer_trig_05/init.py", line 76, in get_processor_info return subprocess.check_output(cmd_lst) File "/usr/local/lib/python3.7/subprocess.py", line 411, in check_output **kwargs).stdout File "/usr/local/lib/python3.7/subprocess.py", line 512, in run output=stdout, stderr=stderr)."

What am I doing wrong? Or maybe there're any other ways to extract the processor id?

CapJS
  • 75
  • 7
  • Why are you posting a photo of the screen instead of copying and pasting text? – Charles Duffy Apr 10 '22 at 20:08
  • More to the point, do you have passwordless sudo privileges for noninteractive processes on the VM in question? If not, you'll need to edit `/etc/sudoers` (hopefully scoped to _only_ allow `dmidecode`) – Charles Duffy Apr 10 '22 at 20:09
  • By the way, you would get more useful error messages if you didn't use `shell=True`, and captured stderr. – Charles Duffy Apr 10 '22 at 20:12
  • @CharlesDuffy unfortunately, i don't know how to check what sudo priviliges i have in VM for my function app (i'm new in Azure), but i guess i don't have such priviliges. Is there a way to get the processor id in another way? P.S. i've edited the post - detailed the error output – CapJS Apr 10 '22 at 20:44
  • 1
    In general, Linux intentionally restricts CPU ID information to protect user privacy. Have you considered using `/etc/machine-id` instead? How about the ethernet MAC address? – Charles Duffy Apr 10 '22 at 20:48
  • 1
    Do also see [Best way to get machine ID on Linux](https://stackoverflow.com/questions/10152762/best-way-to-get-machine-id-on-linux). Because you're running on an emulated processor, you don't have access to a real hardware serial number either way. – Charles Duffy Apr 10 '22 at 20:50
  • (what's the real use case here? I'm curious to figure out what your actual needs are, and which identifiers other than the CPU ID -- if any -- might meet them). – Charles Duffy Apr 10 '22 at 20:53
  • 1
    @CharlesDuffy thanks a lot. I used the command from here: https://stackoverflow.com/a/63148464/9951611 and manged to get machine id. I think machine id will be enough for me – CapJS Apr 10 '22 at 21:13

1 Answers1

0

I guess it's a typo but the command you pass to the subprocess.checkoutput is sudo vim and not sudo dmidecode --type processor. Besides, just like in the Darwin case, arguments should be passed as a list of string. I changed the return value to match the other cases:

import platform, subprocess, logging

def get_processor_info():
    if platform.system() == "Windows":
        return platform.processor()
    elif platform.system() == "Darwin":
        return subprocess.check_output(['/usr/sbin/sysctl', "-n", "machdep.cpu.brand_string"]).strip()
    elif platform.system() == "Linux":
        command = "sudo dmidecode --type processor"
        cmd_lst = command.split(' ')
        return subprocess.check_output(cmd_lst)
    return ""
    
print(get_processor_info())

Edit: like @CharlesDuffy rightfully pointed out, you can match the other cases by writing it this way:

def get_processor_info():
    if platform.system() == "Windows":
        return platform.processor()
    elif platform.system() == "Darwin":
        return subprocess.check_output(['/usr/sbin/sysctl', "-n", "machdep.cpu.brand_string"]).strip()
    elif platform.system() == "Linux":
        return subprocess.check_output(['sudo', 'dmidecode', '--type', 'processor'])
    return ""
Tranbi
  • 11,407
  • 6
  • 16
  • 33
  • I've already changed the command here - I made the mistake when i pasted the code here. But i ran the code with your code and got the same error as in my post. – CapJS Apr 10 '22 at 20:47
  • Better to use `command = ['sudo', 'dmidecode', '--type', 'processor']` -- there's no real advantage to `command.split(' ')`, and several disadvantages to making a habit of it (if you need to parameterize filenames, there are bugs one runs into when they contain spaces when going that route) – Charles Duffy Apr 10 '22 at 20:47
  • @CharlesDuffy you're right. I just wanted to make clear the changes to OP's code. If we want to match the other cases, the right way to write it would even be `return subprocess.check_output(['sudo', 'dmidecode', '--type', 'processor'])` on a single line – Tranbi Apr 10 '22 at 20:49