wociscz / lxd-telegraf-stats

LXD containers metrics
24 stars 14 forks source link

pylxd #4

Open olegsidokhmetov opened 4 years ago

olegsidokhmetov commented 4 years ago

Hello!

I try to start "lxd-telegraf-stats.py" but have some issue with module named "pylxd"

root@lxd0x:/usr/local/sbin# lxd-telegraf-stats.py Traceback (most recent call last): File "/usr/local/sbin/lxd-telegraf-stats.py", line 3, in import pylxd ImportError: No module named pylxd

But "pylxd" already installed root@lxd0x:/usr/local/sbin# pip3 install pylxd Requirement already satisfied: pylxd in /usr/lib/python3/dist-packages (2.2.10) Requirement already satisfied: ws4py!=0.3.5,>=0.3.4 in /usr/lib/python3/dist-packages (from pylxd) (0.4.2)

10101010101010001 commented 4 years ago

I get the same error on Ubuntu 20.04.

10101010101010001 commented 4 years ago

I get the same error on Ubuntu 20.04.

To install on 5.4.0-48-generic you must first install python2.7, set alternative versions for python, Install PIP and dependencies in python 2.7.

$ apt install python2.7

Update python alternative versions (because python3 is default on Ubuntu 20.04) See: https://www.vultr.com/docs/how-to-install-python-2-on-ubuntu-20-04

$ update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 $ update-alternatives --install /usr/bin/python python /usr/bin/python3 2

$ python --version # should be 2.7

Now Install PIP and then use pip to download dependencies. $ wget https://bootstrap.pypa.io/get-pip.py $ python get-pip.py $ pip install pylxd

wociscz commented 4 years ago

Hi, all I've created python3 branch which have modified python script for python3 Tested on ubuntu 20.04 with installed additional python modules via pip3 install ws4py pylxd and looks like it works as expected. As far I am getting reasonable output from my lxd installation. Only removed hddprio metric which seems to not working (don't looked around why).

Feel free to test it and let me know if it works for you, I'll merge it with master branch then.

10101010101010001 commented 4 years ago

Hi, all I've created python3 branch which have modified python script for python3 Tested on ubuntu 20.04 with installed additional python modules via pip3 install ws4py pylxd and looks like it works as expected. As far I am getting reasonable output from my lxd installation. Only removed hddprio metric which seems to not working (don't looked around why).

Feel free to test it and let me know if it works for you, I'll merge it with master branch then.

Its not working for my with python3. It looks like its still relying on pylxd from python 2.7, installing pip with python2.7 and then pylxd as I noted in my last comment solves resolves the error.

wociscz commented 4 years ago

Its not working for my with python3. It looks like its still relying on pylxd from python 2.7, installing pip with python2.7 and then pylxd as I noted in my last comment solves resolves the error.

Hmm that's weird -> did you fetch python3 branch from this repo? It can't rely on python 2.7 bc at the beginning of the script there is #!/usr/bin/env python3 - so it calls python3 (and thus it needs python3 modules). I don't have python2 at all on my ubuntu 20.04 (there is default python3). If you don't need python2, try to uninstall python2 packages. and try pip3 list and look if you have installed proper modules (ws4py & pylxd).

10101010101010001 commented 4 years ago

On 20.04 Python3 is the default and needs to be symlinked to python in order for the script to run on vanilla install. Here is roughly how I tested (all on one box):

`apt update && apt upgrade -y && reboot apt install net-tools wget curl git snap python3-pip -y

snap refresh snap install lxd

git clone https://github.com/wociscz/lxd-telegraf-stats.git

Install telegraf

https://archive.docs.influxdata.com/telegraf/v1.4/introduction/installation/

curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add - source /etc/lsb-release echo "deb https://repos.influxdata.com/${DISTRIB_ID,,} ${DISTRIB_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/influxdb.list sudo apt-get update && sudo apt-get install telegraf

sudo systemctl start telegraf

Install influxdb

https://docs.influxdata.com/influxdb/v1.3/introduction/installation/

curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add - source /etc/lsb-release echo "deb https://repos.influxdata.com/${DISTRIB_ID,,} ${DISTRIB_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/influxdb.list sudo apt-get update && sudo apt-get install influxdb

systemctl start influxdb

Install Grafana

https://grafana.com/docs/grafana/latest/installation/debian/

sudo apt-get install -y apt-transport-https sudo apt-get install -y software-properties-common wget wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add - echo "deb https://packages.grafana.com/enterprise/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list sudo apt-get update sudo apt-get install grafana

sudo systemctl daemon-reload sudo systemctl start grafana-server sudo systemctl enable grafana-server.service sudo systemctl status grafana-server

cp etc/telegraf/telegraf.d/lxd.conf /etc/telegraf/telegraf.d/ cp etc/sudoers.d/telegraf /etc/sudoers.d/ cp usr/local/sbin/lxd-telegraf-stats.py /usr/local/sbin/ chmod u+x /usr/local/sbin/lxd-telegraf-stats.py

systemctl restart influxdb systemctl restart telegraf

Create symlink

ln -s /usr/bin/python3 /usr/bin/python

Get PIP3

cd ~ wget https://bootstrap.pypa.io/get-pip.py python get-pip.py

pip3 install ws4py pip3 install pylxd

python2.7 get-pip.py

pip2 install ws4py pip2 insatll pylxd

/usr/local/sbin/lxd-telegraf-stats.py telegraf --test `

wociscz commented 4 years ago

With the git clone you need to switch to python3 branch afterwards: git clone git@github.com:wociscz/lxd-telegraf-stats.git

Cloning into 'lxd-telegraf-stats'...
remote: Enumerating objects: 30, done.
remote: Counting objects: 100% (30/30), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 71 (delta 8), reused 15 (delta 3), pack-reused 41
Receiving objects: 100% (71/71), 544.82 KiB | 1.86 MiB/s, done.
Resolving deltas: 100% (20/20), done.

now you are in master branch (python2 version) to check you are in python2:

cd lxd-telegraf-stats/
head usr/local/sbin/lxd-telegraf-stats.py
#!/usr/bin/env python

import pylxd
import os
import sys
import json
import multiprocessing
import subprocess
import commands 
import collections

and

git branchroot@nas:~/lxd-telegraf-stats# git branch
* master

now switch to python3 branch

root@nas:~/lxd-telegraf-stats# git checkout python3
Branch 'python3' set up to track remote branch 'python3' from 'origin'.
Switched to a new branch 'python3'
root@nas:~/lxd-telegraf-stats# head usr/local/sbin/lxd-telegraf-stats.py 
#!/usr/bin/env python3

import pylxd
import os
import sys
import json
import multiprocessing
import subprocess
import collections
import re
root@nas:~/lxd-telegraf-stats# git branch
  master
* python3

and as you see, you are in python3 branch, where is python3 version of the script. No need to somehow alias python to python3 - because there is #!/usr/bin/env python3 shebang and it should work out of the box - you need to install modules with pip3 install.

My ubuntu 20.04 was default install (python3 default).

Looks like, you still trying python2 version from master branch.

10101010101010001 commented 4 years ago

Thanks, it worked after switching branches.