paint-brush
I-Python + SNMP = Ixesha langempela le-NAS Disk Temperature Monitor!🌡️ Funda indlela endenze ngayonge@support2minlog
245 ukufunda

I-Python + SNMP = Ixesha langempela le-NAS Disk Temperature Monitor!🌡️ Funda indlela endenze ngayo

nge 2minlog6m2024/10/31
Read on Terminal Reader

Inde kakhulu; Ukufunda

Ndisete inkqubo ethembekileyo yexesha langempela le-NAS HDD yokujonga ubushushu. Iskripthi sePython sisebenza kwindawo yeDocker ngqo kwiNAS. Iqokelela iqondo lokushisa nge-SNMP protocol kwaye ithumela idatha kwi-platform ye-2minlog yokubonisa idatha. Ndibona uphuhliso lobushushu ngeMatplotlib kwaye ndiyibonise kwithebhulethi ye-Android.
featured image - I-Python + SNMP = Ixesha langempela le-NAS Disk Temperature Monitor!🌡️ Funda indlela endenze ngayo
2minlog HackerNoon profile picture
0-item
1-item
2-item

Bendihlala ndizibuza malunga nobushushu be-HDDs kwi-NAS yam. Ndine-NAS kwigumbi elivaliweyo ngaphandle kwe-airco. Ngaphezu koko, ndaqhawula i-NAS kwixesha elidlulileyo, kwaye iidiski zazishushu ngokwenene ... andizange ndilinganise ubushushu ngelo xesha, kodwa ndaqala ukuba nexhala. Unokufumana iingxoxo ezininzi kwi-NAS yobushushu bokuqhuba kwaye uyibeke esweni kwindawo yeLinux/Python. Kodwa akukho nasinye sezicombululo esandincedayo!


Into ebendiyifuna:

  • Ukujongwa kobushushu be-NAS HDD ngendlela ethembekileyo, ebhaliweyo -khange ndifune ukufunda ixabiso ukusuka kwindawo ethile ye-Linux, enokutshintsha ukukhutshwa okulandelayo kwesoftware yeNAS.
  • Bendifuna ukubona ikhaya lam le-NAS kunye ne-NAS yam yokugcina , ekwigumbi elingaphantsi komntakwethu, kwigrafu enye.
  • Ndandifuna ukubona amaxabiso kwigrafu ekhangeleka kakuhle. Ndandifuna ukuguquguquka okupheleleyo kwinkangeleko yegrafu, kungcono ukuyibeka ndedwa eMatplotlib.
  • Ndifuna ukukwazi ukuthatha amaxabiso kwideshibhodi yeNAS kwaye ndibonise iigrafu ngexesha lokwenyani . Ndinomboniso we -2minlog ohleli phezu kwedesika yam. Sele ibonisa ubushushu, ukufuma, kunye namanqanaba ongcoliseko kwibalcony yam kunye nembali yokufumaneka kwam uqhagamshelo lwe-intanethi. Ndifuna ukubona imbali yobushushu beHDD apho.


Inyathelo 1: Ukuqokelela idatha yobushushu

Siza kuqokelela idatha nge -SNMP protocol ngephakheji ye-pysnmp .


Ukuqonda i-SNMP kunye ne-MIBs

I-SNMP (iProtocol yoLawulo lweNethiwekhi elula) yinkqubo esetyenziswa ngokubanzi yokubeka iliso kwimpilo kunye nokusebenza kwezixhobo zenethiwekhi. Ivumela ukuqokelelwa kwedatha efana namaqondo obushushu, ukusetyenziswa kwe-CPU, kunye nesimo sediski.

Ii-MIBs (iZiseko zoLawulo lweeNkcukacha) ziinkcukacha zolwazi ezinokubuzwa ngeSNMP. Inxalenye nganye yedatha ichongiwe yi-OID (Isichongi seNto), echonga ngokukodwa ukuguquguquka okunokufundwa okanye ukusetwa nge-SNMP.


Kufuneka uchaze amaxabiso e-MIB oza kuwaqokelela. NdineSynology NAS. Bapapasha ifayile ye-MIB kumaphepha abo. Kufuneka siqokelele:

  • Igama leDiskhi: 1.3.6.1.4.1.6574.2.1.1.2
  • Imodeli yeDiski: 1.3.6.1.4.1.6574.2.1.1.3
  • Ubushushu beDiski: 1.3.6.1.4.1.6574.2.1.1.6


Kukho i -chatbot ebalaseleyo kwiphepha le-pysnmp. Yabhala umzimba weskripthi sePython kum, iphatha bonke ubunzima ngeSNMP API kunye nokusingatha iifowuni ze-async. Icandelo eliphambili lilandelayo:

 async def run(server_name, ipaddress, username, passwd, outinfo): # SNMP walk for disk name, model, and temperature oids = [ ObjectType(ObjectIdentity('1.3.6.1.4.1.6574.2.1.1.2')), # Disk name (diskID) ObjectType(ObjectIdentity('1.3.6.1.4.1.6574.2.1.1.3')), # Disk model (diskModel) ObjectType(ObjectIdentity('1.3.6.1.4.1.6574.2.1.1.6')) # Disk temperature (diskTemperature) ] errorIndication, errorStatus, errorIndex, varBinds = await bulkCmd( SnmpEngine(), UsmUserData(username, passwd, authProtocol=usmHMACSHAAuthProtocol), # Use the appropriate auth protocol await UdpTransportTarget.create((ipaddress, 161)), ContextData(), 0, 10, # Increase the max-repetitions to get more results in one request *oids # Query disk name, model, and temperature ) if errorIndication: print(f"Error: {errorIndication}") elif errorStatus: print(f"Error Status: {errorStatus.prettyPrint()} at {errorIndex and varBinds[int(errorIndex) - 1] or '?'}") else: disk_data = {} for varBind in varBinds: oid, value = varBind oid_str = str(oid) # Disk name if oid_str.startswith('1.3.6.1.4.1.6574.2.1.1.2'): index = oid_str.split('.')[-1] if index not in disk_data: disk_data[index] = {} disk_data[index]['name'] = value # Disk model elif oid_str.startswith('1.3.6.1.4.1.6574.2.1.1.3'): index = oid_str.split('.')[-1] if index not in disk_data: disk_data[index] = {} disk_data[index]['model'] = value # Disk temperature elif oid_str.startswith('1.3.6.1.4.1.6574.2.1.1.6'): index = oid_str.split('.')[-1] if index not in disk_data: disk_data[index] = {} disk_data[index]['temperature'] = value # Print out the disk information for index, info in disk_data.items(): name = info.get('name', 'Unknown') model = info.get('model', 'Unknown') temperature = info.get('temperature', 'Unknown') name = str(name) model = str(model) temperature = str(temperature) print(f"IP Address {ipaddress}, Disk {index}: Name: {name}, Model: {model}, Temperature: {temperature} °C") outinfo.append({'server_name': server_name, 'ip': ipaddress, 'disk': index, 'name': name, 'model': model, 'temperature': temperature})


Kufuneka wenze iprotocol yeSNMP kwiSynology NAS useto:

Inyathelo lesi-2: Sebenzisa iskripthi kwaye uthumele idatha yokuqhubela phambili

Ndithumele iskripthi ngokuthe ngqo kwi-NAS kwindawo yeDocker. Kuya kufuneka uqinisekise ukuba isikhongozeli seDocker siqala kwakhona emva kokuba uqalise kwakhona. Ndisete ifayile ye-docker-compose.yaml elula ngenxa yeso sizathu:

 version: '3.8' services: pingchart: build: . restart: always container_name: synology-temperature

Emva koko qala iDocker nge docker-compose up -d .


Ndidibene ne -2minlog -inkqubo elula yokuqokelela, inkqubo, kunye nombono wedatha. Uthumela idatha apho ngezicelo ze-HTTPS (ezifakwe kwi-URL okanye emzimbeni) kwaye umise iskripthi sokubonwayo apho. Iigrafu ke zifumaneka ngokulula naphi na apho ufuna khona.


Ungasebenzisa i-2minlog. Kungenjalo, ungathumela idatha kwisiseko sedatha okanye inkqubo yefayile yendawo.

Inyathelo 3: Ukubona idatha

Ndiseta iskripthi esilula seMatplotlib ukubonisa igrafu. Ngokwenyani, ndicele i-ChatGPT (o1-preview) ukuba iyenze, kwaye yenze kakuhle kakhulu. Umbhalo wePython wawungafezekanga, kodwa wawulungile ngokwaneleyo ukugqiba umsebenzi ngokukhawuleza. I-prompt ingezantsi.

 Here is a csv file. Can you write a code: Split data into different graphs by combining the server name and name (eg, DS920+ / Disk 1). Each graph will show the temperature. There will be a title in each graph (eg, DS920+ / Disk 1) The graphs will have the same temperature range. The background will be black, graph background will be also black, the graph color will be from dark green (low temperatures) to light green (high temperatures). There will be two thin lines - 20 °C (blue) and 45 °C (red). Trim the data for last week with tickmarks at midnight of every day. The data are in UTC time. Convert it to Europe/Berlin time zone. The resolution of the total image is hxw 600 x 1024 pixels. Save the image to PNG. disk,ip,model,name,server_name,temperature,timestamp 0,10.0.0.9,ST4000VN008-2DR166,Disk 3,DS920+,38,2024-09-19T20:19:48.723761 1,10.0.0.9,ST16000NM000J-2TW103,Disk 4,DS920+,42,2024-09-19T20:19:49.253975 2,10.0.0.9,ST4000VX007-2DT166,Disk 1,DS920+,38,2024-09-19T20:19:49.818734 3,10.0.0.9,ST4000VX007-2DT166,Disk 2,DS920+,39,2024-09-19T20:19:50.393793 0,10.0.2.9,ST12000NM001G-2MV103,Disk 1,DS220j,28,2024-09-19T20:19:50.873142 0,10.0.0.9,ST4000VN008-2DR166,Disk 3,DS920+,38,2024-09-19T20:20:02.119583 1,10.0.0.9,ST16000NM000J-2TW103,Disk 4,DS920+,42,2024-09-19T20:20:02.596654 2,10.0.0.9,ST4000VX007-2DT166,Disk 1,DS920+,38,2024-09-19T20:20:03.101480 3,10.0.0.9,ST4000VX007-2DT166,Disk 2,DS920+,39,2024-09-19T20:20:03.697423 0,10.0.2.9,ST12000NM001G-2MV103,Disk 1,DS220j,28,2024-09-19T20:20:04.221348 0,10.0.0.9,ST4000VN008-2DR166,Disk 3,DS920+,38,2024-09-19T20:25:02.254611 1,10.0.0.9,ST16000NM000J-2TW103,Disk 4,DS920+,42,2024-09-19T20:25:02.714633 2,10.0.0.9,ST4000VX007-2DT166,Disk 1,DS920+,38,2024-09-19T20:25:03.295622 3,10.0.0.9,ST4000VX007-2DT166,Disk 2,DS920+,39,2024-09-19T20:25:03.780728 ...


Iskripthi sokubonwayo sisetyenziswe ngaphakathi kweqonga le-2minlog. Ungayiqhuba nalapha ekhaya.


Iskripthi siyafumaneka kwi -GitHub .

Ukusonga

Ungasebenzisa i -2minlog elawulwa ngokupheleleyo ukuqokelela, inkqubo, kunye nombono wedatha. Jonga uxwebhu . Ndibonisa iziphumo kwithebhulethi ye-Android ehleli etafileni yam kwaye ndijikeleze ngeegrafu ezahlukeneyo ngeTuner yoMfanekiso . Unako kwakhona ukugcina idatha kwinkqubo yakho yefayile yendawo kwaye wenze okufanayo.


Isisombululo sivavanywa kwi-Synology NAS, kodwa sinokuhlengahlengiswa kwabanye.

IiReferensi:


#Synology #SynologyNAS #Temperature #Monitoring #DataVisualization #Matplotlib #SNMP #2minlog #Python #Docker