ロボクメモ Robojimamemo

ロボットを研究する、ある学生のメモ。日々、勉強したことをメモ代わりに残して共有します(ROS,C++,python,linux,etc...)

linux 使用メモリをros topic としてpublish

使用メモリをros topic としてpublish

グラフでメモリ使用量がどうなっているか評価するときにbagの中にメモリの使用量も入れておこう考えた。

メモ

#!/usr/bin/env python
import rospy
import sys
import os
import commands

from std_msgs.msg import MultiArrayLayout
from std_msgs.msg import MultiArrayDimension
from geometry_msgs.msg import PoseStamped


if __name__ == '__main__':

    args = sys.argv

    pub = rospy.Publisher('memory', PoseStamped, queue_size=10)
    rospy.init_node('system_monitor', anonymous=True)
    r = rospy.Rate(10) 


    while not rospy.is_shutdown():
        msg = PoseStamped()
        msg.header.frame_id = "memory_swap"
        msg.header.stamp = rospy.Time.now()
        memory = commands.getoutput('free -m --si | grep -e Mem -e Swap | while read a b c d e f g; do echo -n "$b $c "; done | tr -d GB').split()
        #msg.pose.orientation.x,msg.pose.orientation.y,msg.pose.orientation.z, msg.pose.orientation.w = commands.getoutput("free -m --si | grep -e Mem -e Swap | while read a b c d e f g; do echo $b $c; done | tr -d GB")
        print memory
        msg.pose.orientation.x = float(memory[0])
        msg.pose.orientation.y = float(memory[1])
        msg.pose.orientation.z = float(memory[2])
        msg.pose.orientation.w = float(memory[3])

        pub.publish(msg)
        r.sleep()

だいぶ無理がある。